Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell sort-object doesn't work as expected

I tried to use sort-object cmdlet to sort processes by Id's here:

Get-Process | Sort-Object -Property Id

and it works well. In any other example I found, sorting works fine, but when I try to sort employees by their employeeID from Active Directory with this one-liner:

Get-QADUser -IncludeAllProperties -SerializeValues | ? {?_.Mail} | select employeeID | sort-object -property employeeID

I get something like this:

11
1104
1105
1185
119
12
...
like image 849
culter Avatar asked Jan 29 '13 13:01

culter


2 Answers

Get-QADUser returns eployeeId as a string, thus sort uses string sorting mechanism. To sort employeeIds as integers - just cast property to this type:

Get-QADUser -IncludeAllProperties | Sort-Object { [int]$_.employeeId } | select Name, employeeid 
like image 181
BartekB Avatar answered Sep 28 '22 10:09

BartekB


Also u can use {$_.employeeId -as [int]}. This dont cause error in null.

I take this solution with "Frode F."

like image 29
Helio Passarelli Avatar answered Sep 28 '22 10:09

Helio Passarelli