<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/wiki/Table.sortNumerically?action=history&amp;feed=atom</id>
	<title>Table.sortNumerically - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/wiki/Table.sortNumerically?action=history&amp;feed=atom"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Table.sortNumerically&amp;action=history"/>
	<updated>2026-05-15T21:25:16Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Table.sortNumerically&amp;diff=75352&amp;oldid=prev</id>
		<title>Kezoto: Created page with &quot;{{Useful Function}} __NOTOC__ This function sorts tables by index numerically from largest to smallest or from smallest to largest. Its also useful for removing gaps from your table. &lt;br&gt; '''Important : WORKS ONLY FOR TABLES WITH NUMERIC INDEXES!''' &lt;br&gt;  ==Syntax== &lt;syntaxhighlight lang=&quot;lua&quot;&gt;table table.sortNumerically( table theTable, bool fromHighestToLowest = false )&lt;/syntaxhighlight&gt;  ===Required Arguments=== * '''theTable''': the table you would like to sort. ===O...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Table.sortNumerically&amp;diff=75352&amp;oldid=prev"/>
		<updated>2022-08-13T07:56:36Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{Useful Function}} __NOTOC__ This function sorts tables by index numerically from largest to smallest or from smallest to largest. Its also useful for removing gaps from your table. &amp;lt;br&amp;gt; &amp;#039;&amp;#039;&amp;#039;Important : WORKS ONLY FOR TABLES WITH NUMERIC INDEXES!&amp;#039;&amp;#039;&amp;#039; &amp;lt;br&amp;gt;  ==Syntax== &amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;table table.sortNumerically( table theTable, bool fromHighestToLowest = false )&amp;lt;/syntaxhighlight&amp;gt;  ===Required Arguments=== * &amp;#039;&amp;#039;&amp;#039;theTable&amp;#039;&amp;#039;&amp;#039;: the table you would like to sort. ===O...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Useful Function}}&lt;br /&gt;
__NOTOC__&lt;br /&gt;
This function sorts tables by index numerically from largest to smallest or from smallest to largest.&lt;br /&gt;
Its also useful for removing gaps from your table.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Important : WORKS ONLY FOR TABLES WITH NUMERIC INDEXES!'''&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;table table.sortNumerically( table theTable, bool fromHighestToLowest = false )&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Required Arguments===&lt;br /&gt;
* '''theTable''': the table you would like to sort.&lt;br /&gt;
===Optional Arguments===&lt;br /&gt;
* '''fromHighestToLowest''': a bool representing whether the table is to be sorted by index from highest to lowest or lowest to highest.&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns sorted table.&lt;br /&gt;
&lt;br /&gt;
===Author===&lt;br /&gt;
Kezoto&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function table.sortNumerically(theTable, fromHighestToLowest)&lt;br /&gt;
    local sortTable = { }&lt;br /&gt;
    for k, v in pairs(theTable) do&lt;br /&gt;
        table.insert(sortTable, {k, v})&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    table.sort(sortTable, function(a,b)&lt;br /&gt;
        local result = a[1]&amp;lt;b[1] &lt;br /&gt;
        if fromHighestToLowest then&lt;br /&gt;
            result = a[1]&amp;gt;b[1]&lt;br /&gt;
        end&lt;br /&gt;
        return result&lt;br /&gt;
    end)&lt;br /&gt;
&lt;br /&gt;
    theTable = {}&lt;br /&gt;
&lt;br /&gt;
    for k,v in ipairs(sortTable) do&lt;br /&gt;
        table.insert(theTable, v[2])&lt;br /&gt;
    end&lt;br /&gt;
    return theTable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The code below will print the table in the correct sorted order.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local theTable = {[3] = &amp;quot;Dino&amp;quot;, [1] = &amp;quot;Doll&amp;quot;, [2] = &amp;quot;Car&amp;quot;,[6] = &amp;quot;Rattle&amp;quot;, [4] = &amp;quot;Dog&amp;quot;, [5] = &amp;quot;Boat&amp;quot;}&lt;br /&gt;
local theTable2 = {[3] = &amp;quot;Dino&amp;quot;, [1] = &amp;quot;Doll&amp;quot;, [9] = &amp;quot;Rattle&amp;quot;, [4] = &amp;quot;Dog&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
local sortedTable = table.sortNumerically(theTable, true)&lt;br /&gt;
iprint(sortedTable) -- {&amp;quot;Rattle&amp;quot;, &amp;quot;Boat&amp;quot;, &amp;quot;Dog&amp;quot;, &amp;quot;Dino&amp;quot;, &amp;quot;Car&amp;quot;, &amp;quot;Doll&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
local sortedTable2 = table.sortNumerically(theTable, false)&lt;br /&gt;
iprint(sortedTable2) -- {&amp;quot;Doll&amp;quot;, &amp;quot;Car&amp;quot;, &amp;quot;Dino&amp;quot;, &amp;quot;Dog&amp;quot;, &amp;quot;Boat&amp;quot;, &amp;quot;Rattle&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
local sortedTable3 = table.sortNumerically(theTable2, false)&lt;br /&gt;
iprint(sortedTable3) -- {&amp;quot;Doll&amp;quot;, &amp;quot;Dino&amp;quot;, &amp;quot;Dog&amp;quot;, &amp;quot;Rattle&amp;quot;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Useful_Functions}}&lt;/div&gt;</summary>
		<author><name>Kezoto</name></author>
	</entry>
</feed>