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
| Function Sort-ObjectType {
<#
.Synopsis
Sort Object by Types
.Description
Sort Object by Types
.Parameter Type
Specifies the type of object
- ToString: [System.String]
- ToInteger: [System.Int32]
- ToVersion: [System.Version]
- ToIPAddress [System.Net.IPAddress]
.Parameter CaseSensistive
.Parameter Descending
.Parameter Unique
.Example
[1]PS> $str += "10","2","5"
[2]PS> $str | Sort-Object # 10 2 5
[3]PS> $str | Sort-ObjectType -Type ToInteger # 2 5 10
.Example
[1]PS> '1.0.1.10','1.0.1.100','1.0.1.9' | Sort-Object -unique
[2]PS> # 1.0.1.10 > 1.0.1.100 > 1.0.1.9
[3]PS> '1.0.1.10','1.0.1.100','1.0.1.9' | Sort-ObjectType -Type ToVersion
[4]PS> # 1.0.1.9 > 1.0.1.10 > 1.0.1.100
.Example
[1]PS> $ips="3.25.20.100","198.10.3.1","1.2.0.3","10.2.0.6"
[2]PS> $ips | Sort-ObjectType -Type ToIPAddress
.Outputs
System.String
#>
#Requires -version 2.0
Param(
[ValidateSet('ToString','ToInteger','ToVersion','ToIPAddress')]
$Type,
[Switch]$CaseSensitive,
[SWitch]$Descending,
[Switch]$Unique
)
$input | Sort-Object -Property {
$ty=$_
Switch($Type) {
'ToString' {[String]$ty}
'ToInteger' {[Int]$ty}
'ToVersion' {[Version]$ty}
'ToIPAddress' {
[byte[]]$ty.split('.')|ForEach{[string]::Format("{0:000}",$_)}
}
default {$ty}
}
} -case:$CaseSensitive -desc:$Descending -uni:$Unique
} |
Partager