Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
WoW
Talk
English
Views
Read
Edit
History
More
Search
Navigation
Home
Random page
Help using wiki
Editions
for WoW
for WildStar
for Solar2D
Documentation
for WoW
for WildStar
Reference
WoW
⦁ FrameXML
⦁ AddOns
⦁ API
⦁ WoW Lua
WildStar
⦁ AddOns
⦁ API
⦁ WildStar Lua
Engine
Tools
What links here
Related changes
Special pages
Page information
Site
Recent Changes
Editing
WoW:API sort
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{luaapi}} From [http://lua-users.org/wiki/TableLibraryTutorial TableLibraryTutorial] of lua-users.org. table.sort(table [, compFunc]) sort(table[, compFunc]) Sort the elements of a table in-place (i.e. alter the table). > t = { 3,2,5,1,4 } > table.sort(t) > = table.concat(t, ", ") -- display sorted values 1, 2, 3, 4, 5 If the table has a specified size only the range specified is sorted, e.g., > t = { 3,2,5,1,4; n=3 } -- construct a table with user size of 3 > table.sort(t) -- sort will be limited by user size > = table.concat(t, ", ") -- only specified size is concatenated as well 2, 3, 5 ;''Warning:'' Setting the range of the sort is no longer possible since table.setn is no longer supported (setting n=3 will not set the size of the table to 3) [[User:Yssaril|Yssaril]] 05:31, 23 March 2008 (UTC) 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, function(a,b) return a<b end) > = table.concat(t, ", ") 1, 2, 3, 4, 5 We can see if we reverse the comparison the sequence order is reversed. > table.sort(t, function(a,b) return a>b end) > = table.concat(t, ", ") 5, 4, 3, 2, 1 ---- ;''Notes:'' : Sorting tables containing non-numeric keys using a custom comparison function does not work. The function will not get called, and the table will be sorted according to some kind of internal function. ==Sorting by Name== There is a user-function you can write that will sort a table by name during a for function. function pairsByKeys (t, f) local a = {} for n in pairs(t) do table.insert(a, n) end table.sort(a, f) local i = 0 -- iterator variable local iter = function () -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end This function will replace the pairs function in a for statement. Example: for title,value in pairsByKeys(randomtable) do DEFAULT_CHAT_FRAME:AddMessage(title..", "..value); end This will print all the variables in randomtable alphabetically!
Summary:
Please note that all contributions to AddOn Studio are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
AddOn Studio Wiki:Copyrights
for details).
Submissions must be written by you, or copied from a public domain or similar free resource (see
AddOn Studio Wiki:Copyrights
for details).
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Apinav
(
edit
)
Template:Editlink
(
edit
)
Template:Luaapi
(
edit
)
Template:Wowlua
(
edit
)