Module:Shared: Difference between revisions

no edit summary
(Add factory method to sortDictionary, so that the key/value names can be specified.)
No edit summary
(One intermediate revision by the same user not shown)
Line 56: Line 56:
     for k, v in pairs(dict) do
     for k, v in pairs(dict) do
     local newValue = factory(k, v) or {key = k, value = v}
     local newValue = nil
    if factory then
    newValue = factory(k, v)
    end
    newValue = newValue or {key = k, value = v}
         table.insert(sortedTable, newValue)
         table.insert(sortedTable, newValue)
     end
     end
Line 88: Line 92:
end
end
end
end
end
-- Makes a deep copy of a table or otherwise object.
-- Yoinked from http://lua-users.org/wiki/CopyTable
function p.deepcopy(orig)
    local copy
    if type(orig) == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end
end


918

edits