Mercurial > wow > askmrrobot
annotate sort.lua @ 24:ca55277502d0 v5
v5
author | yellowfive |
---|---|
date | Wed, 15 Oct 2014 10:32:46 -0700 |
parents | e77e01abce98 |
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 |