sortr (replaces TSortr)


The sortr function sorts the values of a 1-dimensional array or class object of type CImage or CArray into descending numerical order. To sort into ascending order, use sort.

Syntax

Syntax

sorted_table = sortr( table )

bullet.gif    table is a 1-dimensional array or a CImage or CArray object.

bullet.gif    sorted_table is returned.

Remarks

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 1-dimensional array package contains a table.sort() function for sorting arrays but it cannot be used with the script classes mentioned above. Note that the sortr 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 reverse( sortr(table) ). The reverse function is so fast that its computational overhead is negligible.

Examples

The following script sorts a table containing 1 million random numbers having values between 0 and 1. As always for a 1-dimensional array, the numbers in the array have a pixel type of double. On a test machine, sortr completed in 0.45 seconds. Using table.sort() instead of sortr required 1.86 seconds.

t = random( 1000000 )

-- create a table with 1 million numbers

sortr( t )

-- 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 sortr 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.

V = AttachView( "CImageView" )

-- attach the topmost image window

Assert( V and V:Count() > 0 )

-- make sure an image window is on top

sortr( V:GetImage() )

-- sort the current image

Related Topics

Table and Array Functions, sort, reverse


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.