Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Sorting (again)

Reply
 
Old 11-18-2006   #1 (permalink)
Marco Shaw


 
 

Sorting (again)

I have something like:
PS C:\> gc tmp.csv
User1,50
User3,100
User1,20
User1,100

I want to sort on the 1st column *and* 2nd column
PS C:\> gc tmp.csv|sort
User1,100 (<--wrong!)
User1,20
User1,50
User3,100

Do I have to create objects?

Marco



My System SpecsSystem Spec
Old 11-18-2006   #2 (permalink)
Jacques Barathon [MS]


 
 

Re: Sorting (again)

"Marco Shaw" <marco@Znbnet.nb.ca> wrote in message
news:uAW7sM3CHHA.4256@TK2MSFTNGP04.phx.gbl...
>I have something like:
> PS C:\> gc tmp.csv
> User1,50
> User3,100
> User1,20
> User1,100
>
> I want to sort on the 1st column *and* 2nd column
> PS C:\> gc tmp.csv|sort
> User1,100 (<--wrong!)
> User1,20
> User1,50
> User3,100
>
> Do I have to create objects?


You have to use an expression to cast the second field to an int. Ideally
you should import the file as a CSV file and then sort by properties. To
import it as a CSV file you will have to add a header line to your file,
e.g:

User,Size
User1,50
User3,100
User1,20
User1,100

Then you can do this:

PS> $users = import-csv temp.csv
PS> $users | sort user,{[int]$_.size}

User Size
---- ----
User1 20
User1 50
User1 100
User3 100

If you cannot (or don't want to) add a header line to your file, you can
still split and cast as part of the sort like this:

gc temp.csv | sort {$_.split(',')[0]},{[int]$_.split(',')[1]}
User1,20
User1,50
User1,100
User3,100

Hope that helps.

Jacques

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Sorting Vista mail
RE: Sorting XML PowerShell
Sorting in Vista Vista file management
Sorting Live Mail
Sorting contacts Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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