Advertisement
aidenmagrath

Alphabetical Sort Test

Aug 11th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. function pairsByKeys (t, f)
  2. local a = {}
  3. for n in pairs(t) do table.insert(a, n) end
  4. table.sort(a, f)
  5. local i = 0 -- iterator variable
  6. local iter = function () -- iterator function
  7. i = i + 1
  8. if a[i] == nil then return nil
  9. else return a[i], t[a[i]]
  10. end
  11. end
  12. return iter
  13. end
  14.  
  15. lines = {
  16. apple = 3,
  17. banana = 2,
  18. carrot = 1}
  19.  
  20. for name, line in pairsByKeys(lines) do
  21. print(name, line)
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement