mirror of https://github.com/mpv-player/mpv
TOOLS/autoload.lua: implement natural sorting
This commit is contained in:
parent
6694048272
commit
257dbdf06f
|
@ -53,6 +53,37 @@ table.filter = function(t, iter)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- splitbynum and alnumcomp from alphanum.lua (C) Andre Bogus
|
||||||
|
-- Released under the MIT License
|
||||||
|
-- http://www.davekoelle.com/files/alphanum.lua
|
||||||
|
|
||||||
|
-- split a string into a table of number and string values
|
||||||
|
function splitbynum(s)
|
||||||
|
local result = {}
|
||||||
|
for x, y in (s or ""):gmatch("(%d*)(%D*)") do
|
||||||
|
if x ~= "" then table.insert(result, tonumber(x)) end
|
||||||
|
if y ~= "" then table.insert(result, y) end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function clean_key(k)
|
||||||
|
k = (' '..k..' '):gsub("%s+", " "):sub(2, -2):lower()
|
||||||
|
return splitbynum(k)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- compare two strings
|
||||||
|
function alnumcomp(x, y)
|
||||||
|
local xt, yt = clean_key(x), clean_key(y)
|
||||||
|
for i = 1, math.min(#xt, #yt) do
|
||||||
|
local xe, ye = xt[i], yt[i]
|
||||||
|
if type(xe) == "string" then ye = tostring(ye)
|
||||||
|
elseif type(ye) == "string" then xe = tostring(xe) end
|
||||||
|
if xe ~= ye then return xe < ye end
|
||||||
|
end
|
||||||
|
return #xt < #yt
|
||||||
|
end
|
||||||
|
|
||||||
function find_and_add_entries()
|
function find_and_add_entries()
|
||||||
local path = mp.get_property("path", "")
|
local path = mp.get_property("path", "")
|
||||||
local dir, filename = mputils.split_path(path)
|
local dir, filename = mputils.split_path(path)
|
||||||
|
@ -81,15 +112,7 @@ function find_and_add_entries()
|
||||||
end
|
end
|
||||||
return EXTENSIONS[string.lower(ext)]
|
return EXTENSIONS[string.lower(ext)]
|
||||||
end)
|
end)
|
||||||
table.sort(files, function (a, b)
|
table.sort(files, alnumcomp)
|
||||||
local len = string.len(a) - string.len(b)
|
|
||||||
if len ~= 0 then -- case for ordering filename ending with such as X.Y.Z
|
|
||||||
local ext = string.len(get_extension(a)) + 1
|
|
||||||
a = string.sub(a, 1, -ext)
|
|
||||||
b = string.sub(b, 1, -ext)
|
|
||||||
end
|
|
||||||
return string.lower(a) < string.lower(b)
|
|
||||||
end)
|
|
||||||
|
|
||||||
if dir == "." then
|
if dir == "." then
|
||||||
dir = ""
|
dir = ""
|
||||||
|
|
Loading…
Reference in New Issue