Sort a table

Verified
Added by iNTERFACEWARE

Use table.sort() to sort the items within a table

Source Code
   -- sort numbers
   t = {3,2,1}
   table.sort(t)
   trace(t) 
   --> {1,2,3}
   
   -- sort characters
   t = {'c','b','a'}
   table.sort(t)
   trace(t) 
   --> {'a','b','c'} 
   
   -- sort characters
   t = {'3','2','1','c','b','a'}
   table.sort(t)
   trace(t) 
   --> {'1','2','3','a','b','c'}
   
   -- error cannot compare numbers and characters when sorting
   t = {3,2,1,'c','b','a'}
   table.sort(t)
Description
Use table.sort() to sort the items within a table
Usage Details

Use table.sort() to sort the items within a table. Items must all be of the same type, the sort fails if you try to sort a mixture of item types.

Note: Sorting sparse tables/arrays does not work. It produces unpredictable results, and occasionally errors.

How to use the snippet:

  • Paste the desired sort into your script