Module:Shared: Difference between revisions

Add factory method to sortDictionary, so that the key/value names can be specified.
No edit summary
(Add factory method to sortDictionary, so that the key/value names can be specified.)
Line 52: Line 52:
-- Function to sort a dictionary-like structure where items are added like tbl['key'] = value
-- Function to sort a dictionary-like structure where items are added like tbl['key'] = value
-- We need to turn this structure into a table first, in order to sort it.
-- We need to turn this structure into a table first, in order to sort it.
function p.sortDictionary(dict, comparer)
function p.sortDictionary(dict, comparer, factory)
local sortedTable = {}
local sortedTable = {}
     for k, v in pairs(dict) do
     for k, v in pairs(dict) do
        table.insert(sortedTable, {key = k, value = v})
    local newValue = factory(k, v) or {key = k, value = v}
        table.insert(sortedTable, newValue)
     end
     end
      
      
937

edits