Skip to content

Instantly share code, notes, and snippets.

@bjne
Created September 26, 2018 20:14
Show Gist options
  • Save bjne/71e92dc09594e209a6e7197c915ba668 to your computer and use it in GitHub Desktop.
Save bjne/71e92dc09594e209a6e7197c915ba668 to your computer and use it in GitHub Desktop.
lua sort
local t = {
{ name = "abc", sort={ def="after"}},
{ name = "def", sort={ abc="before", foo="before" } },
{ name = "ghi", sort={ def="before", abc="after", def="before" } },
{ name = "123", sort={ def="before", abc="before" } },
{ name = "xyz" },
{ name = "foo", sort={ bar="after"}},
{ name = "baz", sort={ ghi="before"}},
{ name = "bar", sort={ foo ="before", baz="before" }},
}
table.sort(t, function(m1, m2, final)
for i=1,2 do
local a, b = i==1 and m1 or m2, i==1 and m2 or m1
local sort = b.sort and b.sort[a.name]
if sort then
local order = sort
sort = (i == 1 and sort == "after") or (i==2 and sort ~= "after")
print(i, b.name, order, a.name, sort)
if final ~= nil and sort ~= final then
print("CONFLICT", sort, final)
end
final = sort
end
end
if final ~= nil then
print()
end
return final
end)
for _,v in ipairs(t) do
print(v.name)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment