diff --git a/config.lua b/config.lua index 8c4e506..9d03d2d 100644 --- a/config.lua +++ b/config.lua @@ -123,19 +123,13 @@ settings.load = function(self) local entrysize = 22 local previous = nil - local sortedKeys = {} - for key, value in pairs(gui) do - table.insert(sortedKeys, key) - end - - table.sort(sortedKeys, function(a, b) + local sortGeneralFirst = function(a, b) if a == T["General"] then return true end if b == T["General"] then return false end return a < b - end) + end - for _, category in ipairs(sortedKeys) do - local entries = gui[category] + for category, entries in ShaguTweaks.spairs(gui, sortGeneralFirst) do local entry, spacing = 1, 22 local height = 0 diff --git a/helpers.lua b/helpers.lua index f71c174..75f948d 100644 --- a/helpers.lua +++ b/helpers.lua @@ -296,19 +296,18 @@ ShaguTweaks.TimeConvert = function(remaining) end -- http://lua-users.org/wiki/SortedIteration -local function __genOrderedIndex( t ) +local function __genOrderedIndex(t, sortFunc) local orderedIndex = {} for key in pairs(t) do table.insert( orderedIndex, key ) end - table.sort( orderedIndex ) + table.sort(orderedIndex, sortFunc) return orderedIndex end local function orderedNext(t, state) local key = nil if state == nil then - t.__orderedIndex = __genOrderedIndex( t ) key = t.__orderedIndex[1] else for i = 1,table.getn(t.__orderedIndex) do @@ -326,7 +325,12 @@ local function orderedNext(t, state) return end -ShaguTweaks.spairs = function(t) +ShaguTweaks.spairs = function(t, sortFunc) + if sortFunc ~= nil and type(sortFunc) ~= "function" then + error("ShaguTweaks.spairs: expected a function for sortFunc, got " .. type(sortFunc)) + end + + t.__orderedIndex = __genOrderedIndex(t, sortFunc) return orderedNext, t, nil end