View Single Post
07/17/14, 12:00 PM   #3
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
Originally Posted by Khaibit View Post
It seems like you might want to do the following if you want a sort by value primarily, then key as a tie-breaker:

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
  if (a.value == b.value) then
    return (a.key < b.key)
  else
    return (a.value < b.value)
  end
end
As written, if I understand the LUA sorting functions properly, your function will consider element A to be 'before' element B if either the value OR the key is less than B's (if a.value < b.value is false, but a.key < b.key is true, your sort function returns true). If I understand you correctly, you only want the keys to be considered if the values are the same, instead.
My asumption was this:
When any part of a OR statement is true, the other parts are not even executed.
If the value comparision returns true, the key is irrelevant.
If the value coimparision returns false, then the key might tip it.
In all other cases this one should not be smaller.

But I will try to rework it as you sugested. Could be there is something different in Lua about Bool Operator processing too.
  Reply With Quote