TSortR
The TSortR function sorts the values of a data table into descending ("reverse") order. The table can be a Lua array or a class object of type CImage or CArray. To sort into ascending order, from smallest to largest, use TSort.
table = TSortR( table )
CImage = TSortR( CImage )
where
table or CImage contains the data to be sorted.
On success, the modified data collection is returned, otherwise nil is returned.
All argument types except for the CImage class use double precision real numbers with data type "double". The CImage class supports values ranging from byte to double and other data types. Numeric values other than double can be sorted faster by converting their table to a CImage.
The Lua table package contains a built-in table function, table.sort() for sorting arrays. However, this function cannot be used with the CImage and CArray classes. TSortR can be much faster than table.sort() and it also supports CImage and CArray objects. For arrays having 100,000 or fewer points, the difference in speed usually is not noticed unless sorting is repeated many times inside a loop.
The following script sorts a table containing 1 million random numbers having values between 0 and 1. As always for a Lua table, the numbers in the array have a data type of double. In a test on an computer f medium speed, TSort executed in 0.45 seconds while table.sort() required 1.86 seconds.
|
-- create a table with 1 million numbers |
|
-- sort the values into descending order |
The next script sorts the values in a CImage containing 1 million pixels of type ushort (16 bit unsigned integer). On the same computer, TSortR required 0.227 seconds. Before running the script, the target image window was placed on top so that it could be attached to a CImageView object in the script.
|
-- attach the topmost image window |
|
-- make sure an image window is on top |
|
-- sort the image into descending order |