lua-users home
lua-l archive

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


On Mon, Aug 24, 2015 at 7:54 AM, iain morland <iain@iainmorland.net> wrote:
> 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?

Various sort algorithms use only two operations: compare and swap.
For example, bubble sort, merge sort, quick sort - all these use only
compare and swap.

https://en.wikipedia.org/wiki/Quicksort

Swap is generic (at least on a Lua table), so you do not need to
provide a swap function.

To answer your question:  Any compare function can be used.  It all
depends on what order you want as a result.  In other words, you
specify the order you want by providing the compare function of your
choice.

In your example, the compare function depends not only on the two
elements being compared, but also on the values associated (via
myTable) with those two elements.

-Parke