RSS All Posts

RSS PowerShell Posts

Tags

2142 Active Directory Administrativia Announcements Battlefield Blogging Cricket Deployment Deployment4 Get-PSUGUK Group Policy HowTo Linux MDT MDT 2010 Microsoft Deployment Toolkit MSDN Music Permissions Personal PowerGui Power Lines PowerShell PowerShell Groups PowerShell Support PowerShell Tools PowerShell V2 Presentations PSUGAU Quick Tips Scripting SDDL Security Tech Talk Ubuntu User Groups Virtualisation VMware Infrastructure Client WAIK Weekly Poll Windows 7 Windows Automation Installation Kit Windows Server 2003 Windows Server 2008 XML

Archives

Meta


« | Main | »

PowerShell Import-CSV and Sort-Object

By Adam Bell | July 20, 2009

Recently I noticed that when I was sorting data imported from a CSV file, the behaviour wasn’t quite as expected.

$scores = Import-Csv .\scores.csv
$scores | Sort-Object -Property Score -Descending

sort-object

sort-object


I’m pretty sure Julie’s score of 300 should be higher then Homer’s!

$scores | Get-Member

sort-object example 2

sort-object example 2


A quick look shows that even though the score is a number it’s being treated as a system.string. To resovle this we need to change it back to a number. This is done by wrapping it into a code block and recasting as an integar:

$scores | Sort-Object -Property {[int] $_.Score} -Descending

sort-object example 3

sort-object example 3


Fixed! More information can be found in this Hey,Scripting Guy Article.

Topics: PowerShell | No Comments »

Comments