Advanced

Change History

Discuss your script development with others (bugs, optimizations, methods ...)

Message: Sorting table iterator

Changed By: Target.
Change Date: May 15, 2016 11:16AM

Sorting table iterator
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 [b]bold text[/b]

Iterating trough a list by key order. In this case is it the same as IPairs but it can be used with any key
[code]
for k,v in spairs(finds, function(t,a,c) return c < a end) do
print(k,v)
end
[/code]
Iterating trough a list by cache_name in reverse order.
[code]for k,v in spairs(finds, function(t,a,c) return t[c].cache_name < t[a].cache_name end) do
Print(k,v)
end[/code]


The rest of the post is the spairs function copies from stackoverflow but with changed functions names so it works in the checker system
[code]
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 i j = 0
return function()
i = i j = j + 1
if keys[i] j] then
return keys[ij], t[keys[ij]]
end
end
end
[/code]
Changed By: Target.
Change Date: May 14, 2016 05:06PM

Sorting table iterator
I have often ud

http://stackoverflow.com/questions/15706270/sort-a-table-in-lua

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 [b]bold text[/b]

Iterating trough a list by key order. In this case is it the same as IPairs but it can be used with any key
[code]
for k,v in spairs(finds, function(t,a,c) return c < a end) do
print(k,v)
end
[/code]
Iterating trough a list by cache_name in reverse order.
[code]for k,v in spairs(finds, function(t,a,c) return t[c].cache_name < t[a].cache_name end) do
Print(k,v)
end[/code]


The rest of the post is the spairs function copies from stackoverflow but with changed functions names so it works in the checker system
[code]
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 i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
[/code]

Original Message

Author: Target.
Date: May 14, 2016 04:27PM

Sorting table iterator
I have often ud

http://stackoverflow.com/questions/15706270/sort-a-table-in-lua

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 i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end