forked from akpaevj/OneSTools.PS.TechLog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort-TechLog.ps1
More file actions
78 lines (65 loc) · 2.21 KB
/
Sort-TechLog.ps1
File metadata and controls
78 lines (65 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function Sort-TechLog {
[CmdletBinding()]
[OutputType([psobject[]])]
Param (
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
$InputObject,
[Parameter(Mandatory=$true,
Position = 0,
ValueFromPipelineByPropertyName=$true,
HelpMessage = "Property name that will be used for the comparison")]
$Property,
[Parameter(Mandatory=$true,
HelpMessage = "Max quantity of elements should be taken")]
[int]$Top,
[switch]$Descending
)
begin {
$Script:currentCount = 0
$Script:propertyIsScriptBlock = $property -is [scriptblock]
$Script:inputObjects = [psobject[]]::new($Top)
}
process {
if ($currentCount -lt $inputObjects.Count) {
$inputObjects[$currentCount] = $InputObject
$currentCount++
if ($currentCount -eq $inputObjects.Count) {
if ($Descending) {
$inputObjects = $inputObjects | Sort-Object $Property -Descending
}
else {
$inputObjects = $inputObjects | Sort-Object $Property
}
}
}
else {
for ($i = 0; $i -lt $inputObjects.Count; $i++) {
$item = $inputObjects[$i]
$inputValue = $InputObject."$Property"
$itemValue = $item."$Property"
if ($Descending) {
$needShift = $inputValue -ge $itemValue
} else {
$needShift = $inputValue -le $itemValue
}
if ($needShift) {
# shift other items to the right
for ($y = $inputObjects.Count - 2; $y -ge $i; $y--) {
$inputObjects[$y + 1] = $inputObjects[$y]
}
$inputObjects[$i] = $InputObject
break
}
}
}
}
end {
if ($Descending) {
$inputObjects | Sort-Object $Property -Descending
}
else {
$inputObjects | Sort-Object $Property
}
}
}