排序百分比数字时出现小问题。 我刚写了一个简单的脚本来测试:
function get-percent{
$obj1 = New-Object PSObject
$percentUsed1="{0:P0}" -f (100/100)
$obj1 | Add-Member -membertype NoteProperty -Name 'Percent' -Value $percentUsed1
$obj2 = New-Object PSObject
$percentUsed2="{0:P0}" -f (99/100)
$obj2 | Add-Member -membertype NoteProperty -Name 'Percent' -Value $percentUsed2
$obj3 = New-Object PSObject
$percentUsed3="{0:P0}" -f (8/100)
$obj3 | Add-Member -membertype NoteProperty -Name 'Percent' -Value $percentUsed3
$obj4 = New-Object PSObject
$percentUsed4="{0:P0}" -f (70/100)
$obj4 | Add-Member -membertype NoteProperty -Name 'Percent' -Value $percentUsed4
write $obj1
write $obj2
write $obj3
write $obj4
}
get-percent | sort-object 'Percent' -descending |ft
这是我得到的:
99%
8%
70%
100%
通常,我应该:100%,99%,70%...... 有什么想法吗? THX。
答案 0 :(得分:3)
尝试:
get-percent | sort-object { [INT]($_.percent -replace '%') } -descending
以这种方式sort-object
可以按[INT]
排序,而不是像[String]
一样排序。