From 7b09bf7ffc1a927e4d45eb0407ec7024bff2f4d5 Mon Sep 17 00:00:00 2001 From: Christoph Heinrich Date: Fri, 13 Jan 2023 20:51:00 +0100 Subject: 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). --- TOOLS/lua/autoload.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'TOOLS') 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] -- cgit v1.2.3