It looks like you're new here. If you want to get involved, click one of these buttons!

I don't get how the comp function works as the second parameter of table.sort. I want to call my own custom function instead of the standard function, but I don't know how to set up this function. Could someone explain what exactly the comp function does, and how I could make my own function to use in place of the original one?

Tagged:

## Comments

Hello. Here is an example i wrote to sort t , a table of vec3

the last line applies the the sort to table s.

the comVec3 returns true when a is strictly below b.

If a=b it returns false.

That's all about 'comp'.

Because i want to keep what is the original position in of each vector in table t, i have added a 4rth column to s that contains the index i. After the sort i scan the table s to get the indexes i which are now in the good order . Is that clear now?

table.sort sorts a table using the quicksort algorithm (which is not important here).

It iterates over the table, at each step taking two elements from it and comparing them. The comparison function tells the sorting algorithm if the two elements are already in order (true) or not (false).

This is an equivalent of the standard comparison rule:

The other way round:

Now invent a predicate (means comparison function) yourself.

You will have the "oddest" values listed first, then the even values. Note that no statement was made about the order of the numbers within their repective part, just even or odd.