Another sort function for hotkeys

This commit is contained in:
Henrique Santiago 2013-01-25 09:36:57 -02:00
parent 42b05df717
commit 839495fa9b
2 changed files with 19 additions and 36 deletions

View File

@ -49,21 +49,3 @@ function string:explode(sep, limit)
table.insert(t, tmp) table.insert(t, tmp)
return t return t
end 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

View File

@ -253,27 +253,28 @@ function addKeyCombo(messageBox, keyCombo, keySettings)
local label = nil local label = nil
if currentHotkeysList:getChildById(keyCombo) == nil then if currentHotkeysList:getChildById(keyCombo) == nil then
local label = g_ui.createWidget('HotkeyListLabel') local label = g_ui.createWidget('HotkeyListLabel')
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
end
if not add then
currentHotkeysList:addChild(label)
end
end
label:setId(keyCombo) label:setId(keyCombo)
label:setColor(HotkeyColors.text) label:setColor(HotkeyColors.text)
label:setText(keyCombo .. ': ') label:setText(keyCombo .. ': ')
local children = currentHotkeysList:getChildren()
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
end)
for i=1,#children do
if children[i] == label then
currentHotkeysList:insertChild(i, label)
break
end
end
if keySettings then if keySettings then
hotkeyLabelSelectedOnList = label hotkeyLabelSelectedOnList = label
label.keyCombo = keyCombo label.keyCombo = keyCombo