lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

table.sort is described in chapter 20.2 of Programming in Lua. The optional function argument is being called with two elements of the table to be sorted and has to return true if the first of its arguments shall come first in the sorted table.

Note that the function is actually only defined for an array, which is a table with subsequent numbers as indices.

--
Oliver

Am 24.08.2015 um 16:54 schrieb iain morland:
Greetings all,

I am researching the different compare functions that are available for use with the table.sort function.

After some searching online, I was able to use the following as the second argument in table.sort:

function(lhs,rhs) return myTable[lhs] < myTable[rhs] end

In turn, I used this to sort another table of the same size like so:

table.sort(otherTable,function(lhs,rhs) return myTable[lhs] < myTable[rhs] end)

However I couldn’t find reference to the use of "lhs" or “rhs" in the Lua documentation, and feel frustrated that I don’t fully understand what’s happening in the above code.

So I have two questions:

1. Could anyone explain (or direct me to an explanation of) the different compare functions that can be used with table.sort?

2. Are there any documented examples of how these functions work when sorting one table by the result of sorting another table?

Thanks in advance
iain