From 839495fa9b96cdc18e12ee16775b7e83630c8e82 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Fri, 25 Jan 2013 09:36:57 -0200 Subject: [PATCH] Another sort function for hotkeys --- modules/corelib/string.lua | 18 -------------- modules/game_hotkeys/hotkeys_manager.lua | 31 ++++++++++++------------ 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/modules/corelib/string.lua b/modules/corelib/string.lua index 4da2a034..bcff68a5 100644 --- a/modules/corelib/string.lua +++ b/modules/corelib/string.lua @@ -49,21 +49,3 @@ function string:explode(sep, limit) table.insert(t, tmp) return t end - -function string:operatorLess(other) - local selfLower = self:lower() - local otherLower = other:lower() - local selfLen = self:len() - local otherLen = other:len() - local minLen = math.min(selfLen, otherLen) - for i=1,minLen do - local selfByteI = string.byte(selfLower, i) - local otherByteI = string.byte(otherLower, i) - if selfByteI < otherByteI then - return true - elseif selfByteI > otherByteI then - return false - end - end - return selfLen < otherLen -end diff --git a/modules/game_hotkeys/hotkeys_manager.lua b/modules/game_hotkeys/hotkeys_manager.lua index 6ee7bb4d..8d73c262 100644 --- a/modules/game_hotkeys/hotkeys_manager.lua +++ b/modules/game_hotkeys/hotkeys_manager.lua @@ -253,27 +253,28 @@ function addKeyCombo(messageBox, keyCombo, keySettings) local label = nil if currentHotkeysList:getChildById(keyCombo) == nil then local label = g_ui.createWidget('HotkeyListLabel') + label:setId(keyCombo) + label:setColor(HotkeyColors.text) + label:setText(keyCombo .. ': ') local children = currentHotkeysList:getChildren() - if #children == 0 then - currentHotkeysList:addChild(label) - else - local add = false - for i=1,#children do - if keyCombo:operatorLess(children[i]:getId()) then - currentHotkeysList:insertChild(i, label) - add = true - break - end + children[#children+1] = label + table.sort(children, function(a,b) + if a:getId():len() < b:getId():len() then + return true + elseif a:getId():len() == b:getId():len() then + return a:getId() < b:getId() + else + return false end - if not add then - currentHotkeysList:addChild(label) + end) + for i=1,#children do + if children[i] == label then + currentHotkeysList:insertChild(i, label) + break end end - label:setId(keyCombo) - label:setColor(HotkeyColors.text) - label:setText(keyCombo .. ': ') if keySettings then hotkeyLabelSelectedOnList = label label.keyCombo = keyCombo