Setup Factory 10

Table.Sort

Table.Sort

This is the first topic This is the last topic  

Table.Sort

This is the first topic This is the last topic  

OverviewExamples

Table.Sort (

table   SourceTable,

function Compare = nil )

Example 1

Debug.ShowWindow(true);

MyTable = {46,102,12,85};

Table.Sort(MyTable, nil)

for x,y in pairs(MyTable) do

   Debug.Print(y.."\r\n");

end

Sorts the items in "MyTable" in ascending order and then prints the sorted table to the debug window. Here is what would be printed in the debug window:

12

46

85

102

Example 2

function sorter(v1,v2)

   if (v1 > v2)then

       return true;

   else

       return false;

   end

end

 

 

Debug.ShowWindow(true);

MyTable = {"Hockey","Baseball","Football", "Soccer"};

Table.Sort(MyTable, sorter);

for x,y in pairs(MyTable) do

   Debug.Print(y.."\r\n");

end

The first block of code defines a function "sorter" that compares two values and returns true if the first item is greater than the second item. The "sorter" function is then used in the Table.Sort action to sort the items in descending alphabetical order (z - a). Here is what would be printed in the debug window:

Soccer

Hockey

Football

Baseball

See also: Related Actions


Learn More: Indigo Rose Software - Setup Factory - Buy Now - Contact Us