plugins/hiscore: make hiscore.dat sorter stable so everything isn't rearranged every time (nw)

This commit is contained in:
cracyc 2016-07-08 15:44:54 -05:00
parent 28f2a55ad6
commit 3f58aa4548
2 changed files with 36 additions and 2 deletions

View File

@ -6187,7 +6187,7 @@ ninjemak:
dangar:
dangar2:
dangara:
dangarb:
@:maincpu,program,e209,82,00,20
@:maincpu,program,e394,3,00,00
@ -17187,3 +17187,5 @@ percuss:
;Moguchan (by GeoMan)
moguchan:
@:maincpu,program,5eda,6,0,0

View File

@ -107,7 +107,39 @@ for num, entry in ipairs(entries) do
end
end
table.sort(sorted, function(a,b) if a.src and b.src then return a.src < b.src else return false end end)
-- copyright 2010 Uli Schlachter GPLv2
function stable_sort(list, comp)
-- A table could contain non-integer keys which we have to ignore.
local num = 0
for k, v in ipairs(list) do
num = num + 1
end
if num <= 1 then
-- Nothing to do
return
end
-- Sort until everything is sorted :)
local sorted = false
local n = num
while not sorted do
sorted = true
for i = 1, n - 1 do
-- Two equal elements won't be swapped -> we are stable
if comp(list[i+1], list[i]) then
local tmp = list[i]
list[i] = list[i+1]
list[i+1] = tmp
sorted = false
end
end
-- The last element is now guaranteed to be in the right spot
n = n - 1
end
end
stable_sort(sorted, function(a,b) if a.src and b.src then return a.src < b.src else return false end end)
src = "error";