Return to Snippet

Revision: 27975
at June 28, 2010 23:32 by pauloiam


Initial Code
-- "Sorted by key" table iterator 
-- Extracted from http://www.lua.org/pil/19.3.html

function pairsKeySorted(t, f)
    local a = {}    
    for n in pairs(t) do
        table.insert(a, n)
    end    
    table.sort(a, f)
    
    local i = 0      -- iterator variable
    local iter = function ()   -- iterator function
        i = i + 1
        if a[i] == nil then
            return nil
        else
            return a[i], t[a[i]]
        end
    end
    
    return iter
end

Initial URL

                                

Initial Description

                                

Initial Title
"Sorted by key" table iterator

Initial Tags
table, sort

Initial Language
Lua