2

I've managed to get Powershell to sort multiple columns and in different directions;

$prop1 = @{Expression='Priority'; Descending=$true }

$prop2 = @{Expression='Date'; Ascending=$true }

(Import-Csv $BackUpDataPath"WaitList.csv") |
    Sort-Object $prop1, $prop2 |
    Export-Csv $BackUpDataPath"WaitList.csv" -NoType

My problem is that the Priority value is stored as string data and sorts as text so the output to the file is;

Date              Priority

6/28/2016 16:46  2

6/28/2016 16:59  2

6/29/2016 15:27  11

6/28/2016 16:42  1

6/28/2016 16:49  1

I've tried [int]$prop1 and similar ways to change it to an integer but it doesn't seem to work. The Line 6/29/2016 15:27 11 should be first but it isn't. Bare in mind as long as the #'s are less than 10 it sorts fine.

2 Answers 2

2

Try it with

$prop1 = @{Expression={[int]$_.Priority}; Descending=$true }
Sign up to request clarification or add additional context in comments.

5 Comments

That worked after I put the entire section in ()'s ($prop1 = @{Expression={[int]$_.Priority}; Descending=$true }) Funny, I tried similar configs. Just had't gotten to this one. Thanks.
Glad to hear, but it should not be necessary to put parentheses around it.
I had you use the ()'s. With out them I got an error saying "A positional parameter cannot be found that accepts argument '=' ".
I just realized why it didn't wort. I implemented your changes on the 3rd line instead of the initial declaration of $prop1. Thanks again.
Ah ok so you tried to directly pass it to the Sort-Object cmdlet - yeah in this case you have to wrap it in parentheses so it is evaluated as a sub expression.
0

Looking how to convert from string->int i found [int] is not reliably, so I went for another solution. Please try something like this:

"2;2;11;1" -split ";" | %{[int]$Numeric=[convert]::ToInt32($_.toString(), 10) ; add-member -NotePropertyName "Numeric" -NotePropertyValue $Numeric -InputObject $_ ; echo $_} | Sort-Object Numeric

I create an String array, and then split it, for every object I generate the Int32 version of the value and then add a new member to the input object, I print this new object and finally just sort the array by the new property.

I tried to include the script inside the hashtable for the "property" parameter for sort-object but that didn't work :(.

Hope this can solve your problem.

Regards.

1 Comment

Thanks for your reply. DAXaholics solution worked perfectly for me.

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.