Table.Sort

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