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.

Syntax

sorted_table = sort( 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 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.

Examples

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.

t = random( 1000000 )

-- create a table with 1 million numbers

sort( 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 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.

V = AttachView( "CImageView" )

-- attach the topmost image window

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

-- make sure an image window is on top

sort( V:GetImage() )

-- sort the current image

Related Topics

Table and Array Functions, sortr, reverse


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