1

I'd lile to sort a csv in powershell that looks like this:

0.980242;DATABASE_PICTURES/P1040793.JPG
0.167471;DATABASE_PICTURES/P1040805.JPG
0.133792;DATABASE_PICTURES/P1040808.JPG
0.616142;DATABASE_PICTURES/P1040819.JPG
0.0485409;DATABASE_PICTURES/P1040825.JPG
0.17998;DATABASE_PICTURES/P1040826.JPG
0.0428832;DATABASE_PICTURES/P1040837.JPG
0.713708;DATABASE_PICTURES/P7020228.JPG
1;DATABASE_PICTURES//P7020229.JPG

I want to sort the list from highest to lowest number in the first column -->

1;DATABASE_PICTURES//P7020229.JPG
0.980242;DATABASE_PICTURES/P1040793.JPG
0.713708;DATABASE_PICTURES/P7020228.JPG
0.616142;DATABASE_PICTURES/P1040819.JPG
and so on

I tried this script

Import-CSV C:\xampp\htdocs\results\result.csv -Header ('foo', 'bar') -delimiter ';' | Sort-Object foo

but it just doesn't do anything. I've also researched half the internetz, but none of the given codes work or make sense to me. Could somebody give me a hint?

3
  • 1
    sort {[double]$_.foo} -Descending Commented Feb 7, 2015 at 0:20
  • Ok, so it does sort it now(I am an idiot...int), but doesnt write it back into the file. Commented Feb 7, 2015 at 0:26
  • Ok, i found a workaround, nevermind :) Commented Feb 7, 2015 at 0:32

1 Answer 1

2

Just to get the answer in here since no one has up to this point.

The problem you are having is not that the sort is not working but that it is sorting alphabettically and not numerically like you would expect.

Foo                                                                                                                                                                         
---                                                                                                                                                                         
0.0428832                                                                                                                                                                   
0.0485409                                                                                                                                                                   
0.133792                                                                                                                                                                    
0.167471                                                                                                                                                                    
0.17998                                                                                                                                                                     
0.616142                                                                                                                                                                    
0.713708                                                                                                                                                                    
0.980242                                                                                                                                                                    
1        

When all properties are imported using Import-CSV they are typed as string. Output from Get-Member

Bar         NoteProperty System.String Bar=DATABASE_PICTURES/P1040793.JPG
Foo         NoteProperty System.String Foo=0.980242      

Depending on the other needs of your code you can simply use @PetSerAI's suggestion and cast it in the sort.

`| Sort-Object {[double]$_.Foo} -Descending`

If you have other things to do with that data it might be an advantage to change its type after the import so that you don't have to do multiple casts.

$data = Import-CSV C:\xampp\htdocs\results\result.csv -Header ('foo', 'bar') -delimiter ';' | 
    Select-Object -ExcludeProperty Foo @{Name="Foo";Expression={[double]$_.Foo}},* 
$data | Sort-Object foo -Descending

I will assume that you have more properties that just foo and bar so to make it so you don't have too retype all the properties you can just -ExcludeProperty the one that needs special attention and then used a Calculated property to cast the variable as [double]. Now you can Sort and get the expected results.

Bar         NoteProperty System.String Bar=DATABASE_PICTURES/P1040793.JPG
Foo         NoteProperty System.Double Foo=0.980242
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.