mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Implement numeric collator (natural sorting) in file manager (#6378)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user