Difference between revisions of "Table.sort"

From GiderosMobile
m (Text replacement - "<source" to "<syntaxhighlight")
 
(One intermediate revision by one other user not shown)
Line 7: Line 7:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
table.sort(table,comp)
 
table.sort(table,comp)
</source>
+
</syntaxhighlight>
  
 
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:
  
''> t = { 3,2,5,1,4 }''
+
<syntaxhighlight lang="lua">
''> table.sort(t)''
+
> t = { 3,2,5,1,4 }
''> = table.concat(t, ", ")  -- display sorted values''
+
> table.sort(t)
''1, 2, 3, 4, 5''
+
> = table.concat(t, ", ")  -- display sorted values
 +
1, 2, 3, 4, 5
 +
</syntaxhighlight>
  
 
=== Parameters ===
 
=== Parameters ===
 
'''table''': (table) table to sort<br/>
 
'''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/>
 +
 +
=== Examples ===
 +
'''Alphabetical order'''
 +
<syntaxhighlight lang="lua">
 +
local people = {}
 +
people [1] = { name = "bob", age = 26 }
 +
people [2] = { name = "sue", age = 22 }
 +
people [3] = { name = "ann", age = 28 }
 +
table.sort(people, function(a, b)
 +
return a.name < b.name
 +
end)
 +
</syntaxhighlight>
 +
 +
'''Inversed numerical order'''
 +
<syntaxhighlight lang="lua">
 +
local people = {}
 +
people [1] = { name = "bob", age = 26 }
 +
people [2] = { name = "sue", age = 22 }
 +
people [3] = { name = "ann", age = 28 }
 +
table.sort(people, function(a, b)
 +
return a.age > b.age -- inversed
 +
end)
 +
</syntaxhighlight>
  
 
{{Table}}
 
{{Table}}

Latest revision as of 16:35, 24 May 2024

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

Examples

Alphabetical order

local people = {}
people [1] = { name = "bob", age = 26 }
people [2] = { name = "sue", age = 22 }
people [3] = { name = "ann", age = 28 }
table.sort(people, function(a, b)
	return a.name < b.name
end)

Inversed numerical order

local people = {}
people [1] = { name = "bob", age = 26 }
people [2] = { name = "sue", age = 22 }
people [3] = { name = "ann", age = 28 }
table.sort(people, function(a, b)
	return a.age > b.age -- inversed
end)