From 3f58aa4548327df4a6223ed7bf84e614c2cb12e7 Mon Sep 17 00:00:00 2001 From: cracyc Date: Fri, 8 Jul 2016 15:44:54 -0500 Subject: [PATCH] plugins/hiscore: make hiscore.dat sorter stable so everything isn't rearranged every time (nw) --- plugins/hiscore/hiscore.dat | 4 +++- plugins/hiscore/sort_hiscore.lua | 34 +++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/plugins/hiscore/hiscore.dat b/plugins/hiscore/hiscore.dat index 41931cee93f..f1cc8a6757f 100644 --- a/plugins/hiscore/hiscore.dat +++ b/plugins/hiscore/hiscore.dat @@ -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 + + diff --git a/plugins/hiscore/sort_hiscore.lua b/plugins/hiscore/sort_hiscore.lua index 894c787f99e..20a906e6f3b 100644 --- a/plugins/hiscore/sort_hiscore.lua +++ b/plugins/hiscore/sort_hiscore.lua @@ -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";