Last active
April 14, 2021 20:02
-
-
Save cppcooper/f83bb636b2c0ef7ec2495c1738737967 to your computer and use it in GitHub Desktop.
[Lua] pairs functions (sorting iteration)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function spairs3(t, cmp) | |
-- collect the keys | |
local keys = {} | |
for k,v in pairs(job_distributions) do | |
keys[#keys+1] = k | |
--print(k) | |
end | |
utils.sort_vector(keys, nil, cmp) | |
local i = 0 | |
return function() | |
i = i + 1 | |
if keys[i] then | |
return keys[i], t[keys[i]] | |
end | |
end | |
end | |
function safecompare(a,b) | |
if a == b then | |
return 0 | |
elseif tonumber(a) and tonumber(b) then | |
if a < b then | |
return -1 | |
elseif a > b then | |
return 1 | |
end | |
elseif tonumber(a) then | |
return 1 | |
else | |
return -1 | |
end | |
end | |
function twofield_compare(t,v1,v2,f1,f2,cmp1,cmp2) | |
local a = t[v1] | |
local b = t[v2] | |
local c1 = cmp1(a[f1],b[f1]) | |
local c2 = cmp2(a[f1],b[f2]) | |
if c1 == 0 then | |
return c2 | |
end | |
return c1 | |
end | |
for k,v in spairs3(job_distributions, | |
function(a,b) | |
return twofield_compare(job_distributions, | |
a, b, 'cur', 'max', | |
function(a,b) return safecompare(a,b) end, | |
function(a,b) return safecompare(b,a) end) | |
end) | |
do | |
print(v.cur,v.max,k) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment