Filesystem-sort my directory please
Bean, 116 121 118 bytes
Hexdump:
00000000 26 55 cd a0 62 80 40 a0 6f 53 d0 80 d3 d0 80 a0 &UÍ b.@ oSÐ.ÓÐ.
00000010 6f 20 80 0d 21 80 81 00 20 80 92 20 6f 53 d0 80 o ..!... .. oSÐ.
00000020 a0 78 20 80 83 40 a0 5d a0 5e 53 d0 80 d3 a0 62 x ..@ ] ^SÐ.Ó b
00000030 20 5d 20 80 8b 40 a0 5f a0 60 a0 65 a0 6e dd a0 ] ..@ _ ` e nÝ
00000040 61 50 84 d3 a0 62 20 5e 20 65 4e ce a0 5f 80 4c aP.Ó b ^ eNÎ _.L
00000050 a0 60 8c 20 61 80 53 d0 80 d3 50 80 a0 60 20 80 `. a.SÐ.ÓP. ` .
00000060 b9 20 80 b3 53 50 80 a0 61 20 80 b9 a8 dc c4 aa ¹ .³SP. a .¹¨ÜĪ
00000070 a9 a8 dc e4 aa 29 ©¨Üäª)
00000076
Equivalent JavaScript (140 bytes, ignoring added whitespace for formatting):
f=s=>s.split(/(\D*)(\d*)/).concat(s),
_.sort(
(a,b)=>f(a).reduce(
(c,d,i,r,e=f(b)[i])=>
c||d-e||d.toLowerCase().localeCompare(e.toLowerCase())
)
)
Basically a golfed version of the example implementation given in the question.
Click here for the demo!
JavaScript (ES6), 105 110 101 97 82 bytes
Assumes that each numeric element is up to 99 characters long.
a=>a.sort((a,b)=>(F=s=>s.toLowerCase().replace(/\d+/g,n=>n.padStart(99)))(a)>F(b))
Test
let f =
a=>a.sort((a,b)=>(F=s=>s.toLowerCase().replace(/\d+/g,n=>n.padStart(99)))(a)>F(b))
console.log(f([
'Abcd.txt',
'Cat05.jpg',
'Meme00.jpeg',
'abc.txt',
'cat.png',
'cat1.jpeg',
'cat02.jpg',
'meme03-1.jpeg',
'meme03-02.jpg',
'meme04.jpg'
]).join('\n'))
Jelly, 26 bytes
VµŒlµœ&ØDµ?
e€ØDI0;œṗ⁸Ç€µÞ
Try it online! (The footer code, ÇY
, just splits the resulting list of strings with line feeds for legibility)
How?
VµŒlµœ&ØDµ? - Link 1, evaluate a substring: substring
µ µ µ? - ... then / else / if ...
ØD - if: decimal digit characters '0123456789'
œ& - multi-set intersection, effectively "all digits"?
V - then: eval, cast the digit strings to integers
Œl - else: convert to lowercase, cast the char strings to lowercase
e€ØDI0;œṗ⁸Ç€µÞ - Main link: list of filenames
µ - monadic chain separation
Þ - sort the filenames by the key function, given a single filename:
ØD - decimal digit character yield, '0123456789'
e€ - exists in for €ach -> a list of 0s at chars and 1s at digits
I - increments -> 1s and -1s at changeover indexes - 1, 0s elsewhere, 0s
0; - prepend a zero -> 1s and -1s at changeover indexes, 0s elsewhere
⁸ - left argument, the filename
œṗ - partition at the truthy indexes (the 1s and -1s)
Ç€ - call the last link (1) as a monad for €ach substring