Implement numeric collator (natural sorting) in file manager (#6378)

reviewable/pr6392/r1
Galunid 4 years ago committed by GitHub
parent a0e563858e
commit 62daa7b783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -951,6 +951,7 @@ function FileManager:getSortingMenuTable()
local fm = self
local collates = {
strcoll = {_("filename"), _("Sort by filename")},
numeric = {_("numeric"), _("Sort by filename (natural sorting)")},
strcoll_mixed = {_("name mixed"), _("Sort by name mixed files and folders")},
access = {_("date read"), _("Sort by last read date")},
change = {_("date added"), _("Sort by date added")},
@ -992,6 +993,7 @@ function FileManager:getSortingMenuTable()
set_collate_table("modification"),
set_collate_table("size"),
set_collate_table("type"),
set_collate_table("numeric"),
{
text_func = get_collate_percent,
checked_func = function()

@ -193,6 +193,16 @@ function FileChooser:genItemTableFromPath(path)
return a.percent_finished < b.percent_finished
end
elseif self.collate == "numeric" then
-- adapted from: http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
local function addLeadingZeroes(d)
local dec, n = string.match(d, "(%.?)0*(.+)")
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n)
end
sorting = function(a, b)
return tostring(a.name):gsub("%.?%d+", addLeadingZeroes)..("%3d"):format(#b.name)
< tostring(b.name):gsub("%.?%d+",addLeadingZeroes)..("%3d"):format(#a.name)
end
else
sorting = function(a, b)
return a.name < b.name

Loading…
Cancel
Save