lua-users home
lua-l archive

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


If you need to perform complex comparaison, you should avoid doing that in the comparaison functions itself, but should prepare an accessory table of precomputed sort keys, that can be very quickly compared in the custom function and sort only that secondary table ascan index you can use to then index the main table (which can remain unsorted).

The basic table:sort() function does not has any integrated support to generate sort keys, but you may use the custom comparaison function to generate sort keys on the fly for thectwo objects to compare and cache them for further comparaison with other sort keys for other rows of the table.

Le ven. 24 sept. 2021 à 04:50, Marcus Mason <m4gicks@gmail.com> a écrit :
> On top of that, a sorting function really should just be quickly comparing basic object elements and returning a boolean. A sorting function that needs to do advanced things like yielding is highly questionable to begin with.

I don't see anything wrong with yielding in this place in general, but
I can agree this algorithm as written would be hard to rewrite. Is
yielding in an xpcall handler more appropriate than a sorting
function?

> The answer is: you can't, at least not without mountains of extra code

I can use a pure lua sorting function to remedy this :)