Difference between revisions of "Table.sort"

From GiderosMobile
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
'''Available since:''' Gideros 2011.6<br/>
+
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
=== Description ===
+
=== <translate>Description</translate> ===
 
<translate>Sort the elements of a table in-place (i.e. alter the table).  
 
<translate>Sort the elements of a table in-place (i.e. alter the table).  
 
[[Special:MyLanguage/> t = { 3,2,5,1,4 }|> t = { 3,2,5,1,4 }]]
 
[[Special:MyLanguage/> t = { 3,2,5,1,4 }|> t = { 3,2,5,1,4 }]]
Line 12: Line 12:
 
  table.sort(table,comp)
 
  table.sort(table,comp)
 
</source>
 
</source>
=== Parameters ===
+
=== <translate>Parameters</translate> ===
 
'''table''': (table) <translate>table to sort</translate> <br/>
 
'''table''': (table) <translate>table to sort</translate> <br/>
 
'''comp''': (function) <translate>comparison function returning bool comparison result</translate> '''optional'''<br/>
 
'''comp''': (function) <translate>comparison function returning bool comparison result</translate> '''optional'''<br/>

Revision as of 07:26, 24 August 2018

Available since: Gideros 2011.6

Description

Sort the elements of a table in-place (i.e. alter the table). [[Special:MyLanguage/> t = { 3,2,5,1,4 }|> t = { 3,2,5,1,4 }]] [[Special:MyLanguage/> table.sort(t)|> table.sort(t)]] [[Special:MyLanguage/> = table.concat(t, ", ") -- display sorted values|> = table.concat(t, ", ") -- display sorted values]] 1, 2, 3, 4, 5

A comparison function can be provided to customise the element sorting. The comparison function must return a boolean value specifying whether the first argument should be before the second argument in the sequence. The default behaviour is for the < comparison to be made. For example, the following behaves the same as no function being supplied:

 table.sort(table,comp)

Parameters

table: (table) table to sort
comp: (function) comparison function returning bool comparison result optional