As part of my LibConstantMapper I wanted to add a simple sorting of the result set.
My results are well formed arrays, where each entry is a table containing a "key" and "value" index.
I want those nested tables to be sorted - first by value, then by key (in the odd case a duplicate value exists).
As far as I understand table.sort accepts a function. That function takes 2 parameters (element a and b) and returns true if a < b. So I wrote this function:
Lua Code:
local function CompareKeyValuePair(a, b)
--assert valid input
assert(type(a) == "table" and a.key ~= nil and a.value ~= nil, "argument a must be a table containing a 'key' and 'value' string-index")
assert(type(b) == "table" and b.key ~= nil and b.value ~= nil, "argument b must be a table containing a 'key' and 'value' string-index")
--Sort by value first, key second
return (a.value < b.value) or (a.key < b.key)
end
And call it like this:
Lua Code:
if result ~= {} then
table.sort(result, CompareKeyValuePair)
end
I checked with a d() message, it actually goes into the if block.
But the result does not seems to be ordered.
Anything I overlooked?