sort (replaces TSort)
The sort function sorts the values in 1-dimensional array or class object of type CImage or CArray into ascending numerical order. To sort into descending order, use sortr.
sorted_table = sort( table )
table is a 1-dimensional array or a CImage or CArray object.
sorted_table is returned.
The CImage class supports values ranging from byte to double plus other pixel types. Numeric values other than double can be sorted faster by converting their table to a CImage. The lua table package contains a table.sort() function for sorting arrays but it cannot be used with the script classes mentioned above. Note that the sort function is at least 4 times faster than table.sort() when sorting a 1-dimensional array. For arrays having only 50,000 to 100,000 or less, the difference in speed usually is not noticed.
This method sorts the table into ascending order. If you need the table sorted into descending order, use sortr.
The following script sorts a table containing 1 million random numbers having values between 0 and 1. On a test machine, sort completed in 0.45 seconds. Using table.sort() instead of sort required 1.86 seconds.
|
-- create a table with 1 million numbers |
|
-- sort the values |
The next script sorts the values in a CImage containing 1 million pixels of type ushort (16 bit unsigned integer). On the same machine sort required 0.227 seconds. Before running the script, the target image window was placed on top so that it would be attached by the script.
|
-- attach the topmost image window |
|
-- make sure an image window is on top |
|
-- sort the current image |
Table and Array Functions, sortr, reverse
Mira Pro x64 Script User's Guide, Copyright Ⓒ 2024 Mirametrics,
Inc. All Rights Reserved.