feat: better alphanumerical sorting (#426)
This commit is contained in:

committed by
GitHub

parent
68ac697002
commit
e460e8c973
@@ -23,9 +23,9 @@ sort_filenames = (function()
|
|||||||
|
|
||||||
-- Alphanumeric sorting for humans in Lua
|
-- Alphanumeric sorting for humans in Lua
|
||||||
-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
|
-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
|
||||||
local function pad_number(d)
|
local function pad_number(n, d)
|
||||||
local dec, n = d:match('(%.?)0*(.+)')
|
return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d))
|
||||||
return #dec > 0 and ('%.12f'):format(d) or ('%03d%s'):format(#n, n)
|
or ("%03d%s"):format(#n, n)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- In place sorting of filenames
|
--- In place sorting of filenames
|
||||||
@@ -35,7 +35,7 @@ sort_filenames = (function()
|
|||||||
for i, filename in ipairs(filenames) do
|
for i, filename in ipairs(filenames) do
|
||||||
local first_char = filename:sub(1, 1)
|
local first_char = filename:sub(1, 1)
|
||||||
local order = symbol_order[first_char] or default_order
|
local order = symbol_order[first_char] or default_order
|
||||||
local formatted = filename:lower():gsub('%.?%d+', pad_number)
|
local formatted = filename:lower():gsub('0*(%d+)%.?(%d*)', pad_number)
|
||||||
tuples[i] = {order, formatted, filename}
|
tuples[i] = {order, formatted, filename}
|
||||||
end
|
end
|
||||||
table.sort(tuples, function(a, b)
|
table.sort(tuples, function(a, b)
|
||||||
|
Reference in New Issue
Block a user