-1

I can't wrap my head around why record[x] would match up it's key name to a string in an array, then take it's value as the item to index the string by.. Is this some special feature of table.sort?

list = {"b", "c", "a"}
record = {a = 1, b = 2, c = 3}
table.sort(list, function (x, y) return record[x] < record[y] end)
for _, v in ipairs(list) do print(v) end
>a
>b
>c
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Igorio
  • 918
  • 1
  • 7
  • 17
  • What are you asking? The question doesn't make much sense. The code just sorts the values in `list` by the associated values in `record`. – Colonel Thirty Two Jun 17 '14 at 00:38
  • I don't see it as self evident that table.sort has this functionality. The documentation is vague as to how it will sort two records. I wouldn't think it would be sophisticated enough to iterate through a record, then use it's values to sort the list.. – Igorio Jun 17 '14 at 01:07
  • That's what the comparator (the second argument to `table.sort`) is for. It defines what values are less than what another, so that `table.sort` can order them. – Colonel Thirty Two Jun 17 '14 at 01:15
  • Right, it compares two values and sorts them in place. This intuitively makes sense to me when it's evaluating the values of various indicies and reordering them. But I'm just confused how it knows that record.a == list[3] for instance. It just assumes that the record keys correspond to the list values? – Igorio Jun 17 '14 at 01:36
  • The sort function sorts two values from the table given based on the result of the comp function. That function can do anything it wants to determine that order. In this case it is using the value of the two values as looked up in the other table. – Etan Reisner Jun 17 '14 at 02:15

2 Answers2

2

The statement record = {a = 1, b = 2, c = 3} is equivalent to

record = {}
record["a"] = 1
record["b"] = 2
record["c"] = 3

This should make it clear how the values in list map to keys in record.

lhf
  • 70,581
  • 9
  • 108
  • 149
2

(I'll factor out the comparison function to make the explanation easier.)

list = {"b", "c", "a"}
record = {a = 1, b = 2, c = 3}
local function compare(x, y)
  return record[x] < record[y]
end
table.sort(list, compare)

In the function compare, x and y may be any two elements of list. table.sort must call this function many times to figure out which elements are considered less than others. Without compare, table.sort would just use the < operator. As you can see in the code, compare refers to the record table when deciding whether to return true or false. table.sort merely calls compare without knowing anything about record.

luther
  • 5,195
  • 1
  • 14
  • 24
  • I know it works this way, but I don't understand how. Lets say the compare function is fed two values from record: a and b. It compares them, returns true. table.sort takes that 'true' and does what with it? Does it use the anonymous function to create a relational map such as: a < b < c, then uses that mapping to sort list in place once the anonymous function is done? – Igorio Jun 17 '14 at 17:40
  • @RossCharette If the compare function returns true, that means that `a` is less than `b`. What `table.sort` does with the value is up to the implementation, which may differ between Lua, LuaJIT, LuaJ, etc., but for example, if it was bubble sort, returning `true` may mean that the two elements should be swapped. – Colonel Thirty Two Jun 17 '14 at 17:52