G.SortedPairs
From GMod Wiki
| Function | |
| Syntax |
SortedPairs( Table table, Boolean reverse ) Where is this used? |
| Description: | |
| Generates an iterator for traversing tables in key order | |
| Returns: | Function iterator, Table state |
| Part of Library: | Global Functions |
| Realm: |
|
| BBCode Link: | [b][url=http://wiki.garrysmod.com/?title=G.SortedPairs]G.SortedPairs [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] |
Examples
| Description | Iterate through a table in order of its keys |
|---|---|
| Used on | |
| Code | local TestTable = {b=2,c=1,a=3} for k,v in SortedPairs(TestTable) do print(k,v) end |
| Output | a 3
b 2 c 1 |
| Description | Iterate through a table in reverse order of its keys |
|---|---|
| Used on | |
| Code | local TestTable = {b=2,c=1,a=3} for k,v in SortedPairs(TestTable, true) do print(k,v) end |
| Output | c 1
b 2 a 3 |
Additional Notes
- Each time you call this function it's got to make a table and sort it, if you need the sorted table often you might be better off creating a sorted keys table yourself.
- Unlike table.sort you can't give it a comparator function to define your own order.