diff options
author | Christoph Heinrich <christoph.heinrich@student.tugraz.at> | 2023-01-13 20:51:00 +0100 |
---|---|---|
committer | Dudemanguy <random342@airmail.cc> | 2023-01-15 16:45:24 +0000 |
commit | 7b09bf7ffc1a927e4d45eb0407ec7024bff2f4d5 (patch) | |
tree | 3bc00466c6db14d9a09dcc653b136a0ad6540803 /TOOLS/lua | |
parent | 6cdce9e18e4cd199c1342db6e6d1db03828ad860 (diff) | |
download | mpv-7b09bf7ffc1a927e4d45eb0407ec7024bff2f4d5.tar.bz2 mpv-7b09bf7ffc1a927e4d45eb0407ec7024bff2f4d5.tar.xz |
TOOLS/lua/autoload: improve alphanumeric sorting
Currently filenames like `EP.1.v0.1080p.mp4` do not get sorted correctly
(e.g. episode 11 right after episode 1). That is caused by the `.` in
front of the episode number, making it get sorted as if it were
decimals.
The solution is to match the whole real number or integer instead of
matching the integer part and the fractional part separately.
This will regress sorting of numbers with multiple commas where the
length of the individual segments differs between filenames.
Since those are rather uncommon, that is unlikely to be a problem (for
anyone ever).
Diffstat (limited to 'TOOLS/lua')
-rw-r--r-- | TOOLS/lua/autoload.lua | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/TOOLS/lua/autoload.lua b/TOOLS/lua/autoload.lua index c2cac862e4..b4d40b5a7e 100644 --- a/TOOLS/lua/autoload.lua +++ b/TOOLS/lua/autoload.lua @@ -98,14 +98,14 @@ end -- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua function alphanumsort(filenames) - local function padnum(d) - local dec, n = string.match(d, "(%.?)0*(.+)") - return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) + local function padnum(n, d) + return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d)) + or ("%03d%s"):format(#n, n) end local tuples = {} for i, f in ipairs(filenames) do - tuples[i] = {f:lower():gsub("%.?%d+", padnum), f} + tuples[i] = {f:lower():gsub("0*(%d+)%.?(%d*)", padnum), f} end table.sort(tuples, function(a, b) return a[1] == b[1] and #b[2] < #a[2] or a[1] < b[1] |