<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Sorting table iterator</title>
        <description> I have often used the this sorting iterator instead of Pairs and IPairs.
I have use the one described at http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
You have to add function for the sorting. Often adding a lambda function is appropriate. 
Example with finds as it is in the example checker.
It show sorting by the key and by the value. I think those two examples will give enough information to use the iterator in checkers. The lambda function below is  function(t,a,c) instead of the more natural function(t,a,b) used on stackoverflow because that b triggerd BB code bold text

Iterating trough a list by key order. In this case is it the same as IPairs but it can be used with any key 

for k,v in spairs(finds, function(t,a,c) return c &amp;lt; a end) do
    print(k,v)
end
Iterating trough a list by cache_name in reverse order.
for k,v in spairs(finds, function(t,a,c) return t[c].cache_name &amp;lt; t[a].cache_name end) do
    Print(k,v)
end


The rest of the post is the spairs function copies from stackoverflow but with changed functions names so it works in the checker system

function spairs(t, order)
    -- collect the keys
    local keys = {}
    for k in 
	Pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys 
    if order then
        TableSort(keys, function(a,b) return order(t, a, b) end)
    else
        TableSort(keys)
    end

    -- return the iterator function
    local j = 0
    return function()
        j = j + 1
        if keys[j] then
            return keys[j], t[keys[j]]
        end
    end
end
</description>
        <link>/forum/read.php?4,111,111#msg-111</link>
        <lastBuildDate>Thu, 14 May 2026 14:02:36 +0000</lastBuildDate>
        <generator>Phorum 5.2.23</generator>
        <item>
            <guid>/forum/read.php?4,111,43348#msg-43348</guid>
            <title>Re: Sorting table iterator</title>
            <link>/forum/read.php?4,111,43348#msg-43348</link>
            <description><![CDATA[ The last section should be (I changed it from using i to j since [ i ] = italics on forum):<br />
<br />
<pre class="bbcode">
    -- return the iterator function
    local j = 0
    return function()
        j = j + 1
        if keys[j] then
            return keys[j], t[keys[j]]
        end
</pre>]]></description>
            <dc:creator>pieterix</dc:creator>
            <category>Script development</category>
            <pubDate>Thu, 03 Sep 2020 12:39:13 +0000</pubDate>
        </item>
        <item>
            <guid>/forum/read.php?4,111,43308#msg-43308</guid>
            <title>Re: Sorting table iterator</title>
            <link>/forum/read.php?4,111,43308#msg-43308</link>
            <description><![CDATA[ Here&#039;s an updated version of the function, from a script which was updated when the changes came in:<br />
<br />
<pre class="bbcode">
function spairs(t, order)
  -- collect the keys
  local keys = {}
  for k in pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys
    if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
    else
        table.sort(keys)
    end

    -- return the iterator function
    local i = 0
    return function()
        i = i + 1
        if keys<i> then
            return keys<i>, t[keys<i>]
        end
  end
end --function spairs
</i></i></i></pre>]]></description>
            <dc:creator>sumbloke</dc:creator>
            <category>Script development</category>
            <pubDate>Tue, 01 Sep 2020 22:45:44 +0000</pubDate>
        </item>
        <item>
            <guid>/forum/read.php?4,111,43270#msg-43270</guid>
            <title>Re: Sorting table iterator</title>
            <link>/forum/read.php?4,111,43270#msg-43270</link>
            <description><![CDATA[ Target hasn&#039;t been active in a long time (years), sadly. But if you have a code snippet that&#039;s corrected, feel free to throw it in here.<br />
<br />
I am considering and looking into have more of a source controlled environment for both scripts and tags. It would help both with examples, and then there are changes in PGC or GCCOM that requires editing a lot.<br />
<br />
It could also help with getting nicer outputs and minor fixes, including speling errors.]]></description>
            <dc:creator>magma1447</dc:creator>
            <category>Script development</category>
            <pubDate>Tue, 01 Sep 2020 12:04:08 +0000</pubDate>
        </item>
        <item>
            <guid>/forum/read.php?4,111,39191#msg-39191</guid>
            <title>Re: Sorting table iterator</title>
            <link>/forum/read.php?4,111,39191#msg-39191</link>
            <description><![CDATA[ Target., can you update the example to work with the current version of sandbox (standard table.sort instead of TableSort, pairs instead of Pairs, ...)]]></description>
            <dc:creator>Jakuje</dc:creator>
            <category>Script development</category>
            <pubDate>Sun, 26 Apr 2020 20:32:18 +0000</pubDate>
        </item>
        <item>
            <guid>/forum/read.php?4,111,1019#msg-1019</guid>
            <title>Re: Sorting table iterator</title>
            <link>/forum/read.php?4,111,1019#msg-1019</link>
            <description><![CDATA[ Very helpful functions!  Thanks Target!]]></description>
            <dc:creator>travelingGeek</dc:creator>
            <category>Script development</category>
            <pubDate>Sun, 05 Jun 2016 21:08:43 +0000</pubDate>
        </item>
        <item>
            <guid>/forum/read.php?4,111,111#msg-111</guid>
            <title>Sorting table iterator</title>
            <link>/forum/read.php?4,111,111#msg-111</link>
            <description><![CDATA[ I have often used the this sorting iterator instead of Pairs and IPairs.<br />
I have use the one described at <a href="http://stackoverflow.com/questions/15706270/sort-a-table-in-lua"  rel="nofollow">http://stackoverflow.com/questions/15706270/sort-a-table-in-lua</a><br />
You have to add function for the sorting. Often adding a lambda function is appropriate. <br />
Example with finds as it is in the example checker.<br />
It show sorting by the key and by the value. I think those two examples will give enough information to use the iterator in checkers. The lambda function below is  function(t,a,c) instead of the more natural function(t,a,b) used on stackoverflow because that b triggerd BB code <b>bold text</b><br />
<br />
Iterating trough a list by key order. In this case is it the same as IPairs but it can be used with any key <br />
<pre class="bbcode">
for k,v in spairs(finds, function(t,a,c) return c &lt; a end) do
    print(k,v)
end</pre>
Iterating trough a list by cache_name in reverse order.<br />
<pre class="bbcode">for k,v in spairs(finds, function(t,a,c) return t[c].cache_name &lt; t[a].cache_name end) do
    Print(k,v)
end</pre>
<br />
<br />
The rest of the post is the spairs function copies from stackoverflow but with changed functions names so it works in the checker system<br />
<pre class="bbcode">
function spairs(t, order)
    -- collect the keys
    local keys = {}
    for k in 
	Pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys 
    if order then
        TableSort(keys, function(a,b) return order(t, a, b) end)
    else
        TableSort(keys)
    end

    -- return the iterator function
    local j = 0
    return function()
        j = j + 1
        if keys[j] then
            return keys[j], t[keys[j]]
        end
    end
end
</pre>]]></description>
            <dc:creator>Target.</dc:creator>
            <category>Script development</category>
            <pubDate>Sat, 14 May 2016 14:27:23 +0000</pubDate>
        </item>
    </channel>
</rss>
