Max Power
Max Power

Reputation: 350

Ordering numbered directories that are delimited with a fullstop

I have a query that returns a list of folders within a path:

Get-ChildItem "." -Directory -Filter "*"| Select -Property Name | Sort-Object Name -Descending

In the results for this I get a list of objects:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        30/06/2016     09:51            1.3.35.6503
d----        14/12/2015     11:04            1.3.29.5885                                                                                                                                         
d----        16/11/2015     09:54            1.3.28.5790                                                                                                                                         
d----        27/06/2016     19:33            1.18.3.6497                                                                                                                                         
d----        21/06/2016     19:33            1.18.2.6492                                                                                                                                         
d----        21/06/2016     11:55            1.18.1.6490                                                                                                                                         
d----        10/05/2016     19:34            1.17.99.6427   

This is in an order I wasn't expecting! Looking at windows explorer, it does put them in the correct order if I sort the column by descending.

It would appear to be sorting them by each individually as text, whereas Windows Explorer is a bit more clever and doing it via strings.

Is there an easy way in PowerShell to get this order into the correct one (i.e. like windows explorer)? I'm thinking about a function to return the list and add create a new object with the FullName and Name by splitting the original name up into 4 elements and sorting by those individually.

Upvotes: 2

Views: 33

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59001

You want to sort the files by a Version. You don't need to split the string, just cast the BaseName to a version and sort it:

Get-ChildItem '.' -Directory -Filter "*" | sort { [version]$_.BaseName }

Result:

Mode                LastWriteTime         Length Name                                                                                                                                     
----                -------------         ------ ----                                                                                                                                     
-a----       30.06.2016     13:15              0 1.3.28.5790.txt                                                                                                                          
-a----       30.06.2016     13:15              0 1.3.29.5885.txt                                                                                                                          
-a----       30.06.2016     13:15              0 1.3.35.6503.txt                                                                                                                          
-a----       30.06.2016     13:15              0 1.17.99.6427.txt                                                                                                                         
-a----       30.06.2016     13:15              0 1.18.1.6490.txt                                                                                                                          
-a----       30.06.2016     13:15              0 1.18.2.6492.txt                                                                                                                          
-a----       30.06.2016     13:15              0 1.18.3.6497.txt 

Upvotes: 3

Related Questions