Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active February 5, 2024 12:16
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save markwragg/e2a9dc05f3464103d6998298fb575d4e to your computer and use it in GitHub Desktop.
PowerShell natural sort. A regex way to sort files that have a number in them correctly, e.g rather than img1, img10, img11, img2, -> img1, img2, img10, img11
# http://stackoverflow.com/a/5429048/2796058
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
Get-ChildItem | Sort-Object $ToNatural
@illusive-man
Copy link

Concise (and working) one liner for natural sorting. Thanks for pointing out! Google has shown your gist way above SO post :)

@Raiu
Copy link

Raiu commented Aug 14, 2020

Hi

I dont know if u can provide any help with your snippet.

For some reason it wont sort childitem list for me.

PS version: 5.1
LanguageMode: ConstrainedLanguage

$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
Get-ChildItem 
Get-ChildItem | Sort-Object $ToNatural

Default

-a---- 2020-08-12 14:15 13820130 1.JPG
-a---- 2020-08-12 14:10 9866069 10.JPG
-a---- 2020-08-12 14:11 11223833 11.JPG
-a---- 2020-08-12 14:11 11551986 12.JPG
-a---- 2020-08-12 14:05 14038655 13.JPG
-a---- 2020-08-12 14:05 12190994 14.JPG
-a---- 2020-08-12 14:11 13169207 15.JPG
-a---- 2020-08-12 13:58 15110531 2.JPG
-a---- 2020-08-12 14:01 11411103 3.JPG
-a---- 2020-08-12 14:01 9129372 4.JPG
-a---- 2020-08-12 14:02 13968575 5.JPG
-a---- 2020-08-12 14:03 12166782 6.JPG
-a---- 2020-08-12 14:03 12604245 7.JPG
-a---- 2020-08-12 14:09 13944348 8.JPG
-a---- 2020-08-12 14:10 10976320 9.JPG

After "Natural Sort"

-a---- 2020-08-12 14:15 13820130 1.JPG
-a---- 2020-08-12 14:10 9866069 10.JPG
-a---- 2020-08-12 14:11 11223833 11.JPG
-a---- 2020-08-12 14:11 11551986 12.JPG
-a---- 2020-08-12 14:05 14038655 13.JPG
-a---- 2020-08-12 14:05 12190994 14.JPG
-a---- 2020-08-12 14:11 13169207 15.JPG
-a---- 2020-08-12 13:58 15110531 2.JPG
-a---- 2020-08-12 14:01 11411103 3.JPG
-a---- 2020-08-12 14:01 9129372 4.JPG
-a---- 2020-08-12 14:02 13968575 5.JPG
-a---- 2020-08-12 14:03 12166782 6.JPG
-a---- 2020-08-12 14:03 12604245 7.JPG
-a---- 2020-08-12 14:09 13944348 8.JPG
-a---- 2020-08-12 14:10 10976320 9.JPG

Testing with letters prepending numbers

-a---- 2020-08-12 14:41 15139084 a1.JPG
-a---- 2020-08-12 14:48 10117130 a10.JPG
-a---- 2020-08-12 14:48 10650048 a11.JPG
-a---- 2020-08-12 14:48 11617851 a12.JPG
-a---- 2020-08-12 14:45 12369061 a13.JPG
-a---- 2020-08-12 14:45 12138366 a14.JPG
-a---- 2020-08-12 14:42 15177245 a2.JPG
-a---- 2020-08-12 14:43 9395040 a3.JPG
-a---- 2020-08-12 14:43 10419256 a4.JPG
-a---- 2020-08-12 14:44 13149593 a5.JPG
-a---- 2020-08-12 14:44 12191723 a6.JPG
-a---- 2020-08-12 14:44 10749331 a7.JPG
-a---- 2020-08-12 14:47 10897389 a8.JPG
-a---- 2020-08-12 14:47 11051948 a9.JPG

Result

-a---- 2020-08-12 14:41 15139084 a1.JPG
-a---- 2020-08-12 14:48 10117130 a10.JPG
-a---- 2020-08-12 14:48 10650048 a11.JPG
-a---- 2020-08-12 14:48 11617851 a12.JPG
-a---- 2020-08-12 14:45 12369061 a13.JPG
-a---- 2020-08-12 14:45 12138366 a14.JPG
-a---- 2020-08-12 14:42 15177245 a2.JPG
-a---- 2020-08-12 14:43 9395040 a3.JPG
-a---- 2020-08-12 14:43 10419256 a4.JPG
-a---- 2020-08-12 14:44 13149593 a5.JPG
-a---- 2020-08-12 14:44 12191723 a6.JPG
-a---- 2020-08-12 14:44 10749331 a7.JPG
-a---- 2020-08-12 14:47 10897389 a8.JPG
-a---- 2020-08-12 14:47 11051948 a9.JPG

@PureOcean
Copy link

One-line in CMD:

powershell -Command "(Get-ChildItem | Sort-Object { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(9999) }) }).Name"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment