2

在排序百分比数字时遇到了一个小问题。我只是写了一个简单的脚本来测试:

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%……有什么想法吗?谢谢。

4

1 回答 1

3

尝试:

get-percent | sort-object  { [INT]($_.percent -replace '%')  } -descending 

这样可以像以前那样sort-object排序[INT]而不是排序。[String]

于 2013-07-10T09:06:21.283 回答