Advertisement
aidenmagrath

Table Keys

Aug 11th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 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. t = load("dictionary")
  16.  
  17. for name, line in pairsByKeys(t) do
  18. print(name, line)
  19. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement