table.SortByMember

From GMod Wiki

Jump to: navigation, search
Function
Syntax table.SortByMember( Table table, String member, Function comparative )
Where is this used?
Description:
Sorts a table into descending order by member.
Returns: nil
Part of Library: Table
Realm: NewerShared.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=Table.SortByMember]Table.SortByMember [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Example

DescriptionSort the values of a Table by member.
Used onNewerServer.png
Code
local playerinfo = {}
local playertable = {}
 
for _,player in pairs(player.GetAll()) do
  playerinfo = {Name = player:Nick(), ID = player:EntIndex()} // This creates a table with the members "Player" and "ID."
  table.insert(playertable, playerinfo) // This inserts a player's information ("Name" and "ID") for each player into "playertable."
end
 
table.SortByMember(playertable, "ID") // Sort the table numerically by the member "ID". This seems to be in descending order.
PrintTable(playertable) // Print the table into our console.
 
 
// If you wish to sort the table in ascending order, then do this.
 
table.SortByMember(playertable, "ID", function(a, b) return a > b end)
 
PrintTable(playertable); // Print the table into our console.
 
 
OutputPrints a Table in order by member into the console.


Additional Notes

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox