Mercurial > wow > askmrrobot
annotate sort.lua @ 17:e77e01abce98
Warlords of Draenor pre-patch
author | Adam tegen <adam.tegen@gmail.com> |
---|---|
date | Mon, 13 Oct 2014 21:28:32 -0500 |
parents | ec731d2fe6ba |
children |
rev | line source |
---|---|
adam@0 | 1 local _, AskMrRobot = ... |
adam@0 | 2 |
adam@0 | 3 function AskMrRobot.spairs(t, order) |
adam@0 | 4 -- collect the keys |
adam@0 | 5 local keys = {} |
adam@0 | 6 for k in pairs(t) do keys[#keys+1] = k end |
adam@0 | 7 |
adam@0 | 8 -- if order function given, sort by it by passing the table and keys a, b, |
adam@0 | 9 -- otherwise just sort the keys |
adam@0 | 10 if order then |
adam@0 | 11 table.sort(keys, function(a,b) return order(t, a, b) end) |
adam@0 | 12 else |
adam@0 | 13 table.sort(keys) |
adam@0 | 14 end |
adam@0 | 15 |
adam@0 | 16 -- return the iterator function |
adam@0 | 17 local i = 0 |
adam@0 | 18 return function() |
adam@0 | 19 i = i + 1 |
adam@0 | 20 if keys[i] then |
adam@0 | 21 return keys[i], t[keys[i]] |
adam@0 | 22 end |
adam@0 | 23 end |
adam@0 | 24 end |