G
G
Gagatyn2018-05-01 00:31:23
PowerShell
Gagatyn, 2018-05-01 00:31:23

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

I tried sorting with dir 5 | sort {[int]$_.name} , but it doesn't exit. How to sort ascending/descending (top list)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Tsvetkov, 2018-05-01
@tsklab

1, 1, 2, 2, 10, 10, 22, 23, 100
Interesting, but that's how files are sorted
in Explorer
5ae84651f363e995944001.png
in FAR
5ae846b262f0b569932915.png

A
azarij, 2018-05-01
@azarij

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.

L
lololololo, 2018-05-13
@lololololo

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 question

Ask a Question

731 491 924 answers to any question