view sort.lua @ 18:fd135f065e75 v2

Added tag v2.0.0.0 for changeset e77e01abce98
author Adam tegen <adam.tegen@gmail.com>
date Mon, 13 Oct 2014 21:31:37 -0500
parents e77e01abce98
children
line wrap: on
line source
local _, AskMrRobot = ...

function AskMrRobot.spairs(t, order)
    -- collect the keys
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys 
    if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
    else
        table.sort(keys)
    end

    -- return the iterator function
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end