adam@0: local _, AskMrRobot = ... adam@0: adam@0: function AskMrRobot.spairs(t, order) adam@0: -- collect the keys adam@0: local keys = {} adam@0: for k in pairs(t) do keys[#keys+1] = k end adam@0: adam@0: -- if order function given, sort by it by passing the table and keys a, b, adam@0: -- otherwise just sort the keys adam@0: if order then adam@0: table.sort(keys, function(a,b) return order(t, a, b) end) adam@0: else adam@0: table.sort(keys) adam@0: end adam@0: adam@0: -- return the iterator function adam@0: local i = 0 adam@0: return function() adam@0: i = i + 1 adam@0: if keys[i] then adam@0: return keys[i], t[keys[i]] adam@0: end adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.sortSlots(t) adam@0: return AskMrRobot.spairs(t, function(x, a, b) adam@0: if a == nil and b == nil then return 0 end adam@0: return AskMrRobot.sortedSlots[a] < AskMrRobot.sortedSlots[b] adam@0: end) adam@0: end