Garry's Mod Wiki

Revision Difference

table.sort#517893

<function name="sort" parent="table" type="libraryfunc"> <description>Sorts a table either ascending or by the given sort function.</description> <realm>Shared and Menu</realm> <args> <arg name="tbl" type="table">The table to sort.</arg> <arg name="sorter" type="function">If specified, the function will be called with 2 parameters each.&#xA;Return true in this function if you want the first parameter to come first in the sorted array.</arg>⤶ <arg name="sorter" type="function">If specified, the function will be called with 2 parameters each.Return true in this function if you want the first parameter to come first in the sorted array.</arg>⤶ </args> </function> <example> <description>Sorting table by an integer</description> <code> local TABLE = { {"Jeff",8}, {"Peter",17}, {"Shay",11}, {"Janine",1} } table.sort(TABLE,function(a, b) return a[2] &amp;gt; b[2] end) table.sort(TABLE,function(a, b) return a[2] &gt; b[2] end) </code> <output>Table going from highest number to lowest (1: Peter, 2: Shay, 3: Jeff, 4: Janine)</output> </example> <example> <description>Sorting a player table by a NWInt</description> <code> local PLAYERS = player.GetAll() table.sort(PLAYERS,function(a, b) return a:GetNWInt("Score") &amp;gt; b:GetNWInt("Score") end) table.sort(PLAYERS,function(a, b) return a:GetNWInt("Score") &gt; b:GetNWInt("Score") end) </code> <output>Player table sorted by score going from highest to lowest</output> </example>