Difference between revisions of "Table.sort"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 +
'''Class:''' [[table]]<br/>
 +
 
=== Description ===
 
=== Description ===
Sort the elements of a table in-place (i.e. alter the table).  
+
Sorts the elements of a table in-place (i.e. alter the table).
`> t = { 3,2,5,1,4 }`
+
<syntaxhighlight lang="lua">
`> table.sort(t)`
+
table.sort(table,comp)
`> = table.concat(t, ", ") -- display sorted values`
+
</syntaxhighlight>
`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:
 
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:
<source lang="lua">
+
 
  table.sort(table,comp)
+
''> t = { 3,2,5,1,4 }''
</source>
+
  ''> table.sort(t)''
'''table''': (table) table to sort ''''''<br/>
+
''> = table.concat(t, ", ") -- display sorted values''
 +
''1, 2, 3, 4, 5''
 +
 
 +
=== Parameters ===
 +
'''table''': (table) table to sort<br/>
 
'''comp''': (function) comparison function returning bool comparison result '''optional'''<br/>
 
'''comp''': (function) comparison function returning bool comparison result '''optional'''<br/>
 +
 +
{{Table}}

Latest revision as of 15:33, 13 July 2023

Available since: Gideros 2011.6
Class: table

Description

Sorts the elements of a table in-place (i.e. alter the table).

table.sort(table,comp)

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:

> t = { 3,2,5,1,4 }
> table.sort(t)
> = table.concat(t, ", ")  -- display sorted values
1, 2, 3, 4, 5

Parameters

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