Answer the question
In order to leave comments, you need to log in
How to sort numbers not by the first digit?
Hello! There are pictures. Need to sort them
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 30.04.2018 23:23 0 1.jpg
-a--- 30.04.2018 23:34 0 1.png
-a--- 30.04.2018 23:40 0 10.jpg
-a--- 30.04.2018 23:39 0 10.png
-a--- 30.04.2018 23:39 0 100.png
-a--- 30.04.2018 23:23 0 2.jpg
-a--- 30.04.2018 23:34 0 2.png
-a--- 30.04.2018 23:39 0 22.png
-a--- 30.04.2018 23:40 0 23.jpg
Answer the question
In order to leave comments, you need to log in
1, 1, 2, 2, 10, 10, 22, 23, 100Interesting, but that's how files are sorted
simply, in this particular case, you need to remove everything that is not a number from the name:
dir c:\test\pstest | sort {[int]($_.name -replace "\..*")}
the same result will be if you change the regular expression like this:
dir c:\test\pstest | sort {[int]($_.name -replace "\D")}
if the file naming scheme changes, then the regular expression will also need to be changed.
and for reverse sorting, sort-object has the -descending key. just add it to the end of the line.
Obviously, you don't need to name files like that - only explorer on Windows sorts them in numerical order. Pad numbers with zeros.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question