This is my Perl script from the other thread with a secondary sort level:
- 1st sort: everything up to the last dot (i.e. the filename root) by using the greedy variant
(.+)\.
of the regex - 2nd sort: the whole string
[example] Sort Lines (Perl ST with Secondary Sort).kmmacros (2.1 KB)
The script:
perl -s -e 'print join( "\n", map { $_->[0] } sort { lc($a->[1]) cmp lc($b->[1]) || lc($a->[0]) cmp lc($b->[0]) } map { [$_, /(.+)\./] } split("\n", $i) )' -- -i="$KMVAR_tmp"
or more readable:
perl -s -e '
print
join(
"\n", map { $_->[0] }
sort {
lc($a->[1]) cmp lc($b->[1]) ||
lc($a->[0]) cmp lc($b->[0])
}
map { [$_, /(.+)\./] }
split("\n", $i)
)
' -- -i="$KMVAR_tmp"
By the way, @JMichaelTX , do you remember that "nice" topic about getting the root domain out of domain names like "files.google.co.uk" or "www.cs.tut.fi" ?
Eventually we'll run into the same problem here:
Sorting is relatively easy if all file names have exactly one extension:
photo.2017.09.18.png [ext.: .png]
photo.original.tiff [ext.: .tiff]
photo.jpg [ext.: .jpg]
You get the "semantically" relevant part, i.e. the file name root, simply by going always for the last dot.
But similar to multi-part TLDs (co.uk, etc.) we have also file names with multiple extensions:
photo.2017.09.18.png [ext.: .png]
photo.original.tiff.zip [ext.: .tiff.zip]
photo.original.gray.tiff [ext.: .tiff]
photo.tar.xz [ext.: .tar.xz]
So I guess, without knowledge of valid file name extensions there is no way to solve this, not via regex, not via splitting.