Skip to content

Instantly share code, notes, and snippets.

@p0pr0ck5
Created March 23, 2017 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p0pr0ck5/675d5062f84cf5e04fd311dbb7f36c3f to your computer and use it in GitHub Desktop.
Save p0pr0ck5/675d5062f84cf5e04fd311dbb7f36c3f to your computer and use it in GitHub Desktop.
$ cat t.lua
local c = require "cjson"
local d = c.decode
local e = c.encode
local j1 = [[
[
{
"weight": 0,
"id": "38b4ab34-f6da-4f88-9e03-aa53f75ed9f7",
"target": "1.2.3.4:8000",
"created_at": 1490116516932,
"upstream_id": "413ea7ee-2740-445d-9c2e-d042e2c7a798"
},
{
"weight": 20,
"id": "8676ddb9-cd96-40ed-a537-7cc714faac44",
"target": "1.2.3.4:8000",
"created_at": 1490116516932,
"upstream_id": "413ea7ee-2740-445d-9c2e-d042e2c7a798"
}
]
]]
local j2 = [[
[
{
"weight": 0,
"id": "38b4ab34-f6da-4f88-9e03-aa53f75ed9f7",
"target": "1.2.3.4:8000",
"created_at": 1490116516932,
"upstream_id": "413ea7ee-2740-445d-9c2e-d042e2c7a798"
},
{
"weight": 20,
"id": "196be764-8fbe-4332-a1d4-c192c7cd6bb5",
"target": "1.2.3.4:8000",
"created_at": 1490116516932,
"upstream_id": "413ea7ee-2740-445d-9c2e-d042e2c7a798"
}
]
]]
local t1 = d(j1)
for _, target in ipairs(t1) do
target.order = target.target..":"..target.created_at..":"..target.id
end
table.sort(t1, function(a, b) return a.order > b.order end)
for _, target in ipairs(t1) do
print(e(target))
end
print("\n")
local t2 = d(j2)
for _, target in ipairs(t2) do
target.order = target.target..":"..target.created_at..":"..target.id
end
table.sort(t2, function(a, b) return a.order > b.order end)
for _, target in ipairs(t2) do
print(e(target))
end
$ resty ./t.lua
{"order":"1.2.3.4:8000:1490116516932:8676ddb9-cd96-40ed-a537-7cc714faac44","target":"1.2.3.4:8000","id":"8676ddb9-cd96-40ed-a537-7cc714faac44","weight":20,"created_at":1490116516932,"upstream_id":"413ea7ee-2740-445d-9c2e-d042e2c7a798"}
{"order":"1.2.3.4:8000:1490116516932:38b4ab34-f6da-4f88-9e03-aa53f75ed9f7","target":"1.2.3.4:8000","id":"38b4ab34-f6da-4f88-9e03-aa53f75ed9f7","weight":0,"created_at":1490116516932,"upstream_id":"413ea7ee-2740-445d-9c2e-d042e2c7a798"}
{"order":"1.2.3.4:8000:1490116516932:38b4ab34-f6da-4f88-9e03-aa53f75ed9f7","target":"1.2.3.4:8000","id":"38b4ab34-f6da-4f88-9e03-aa53f75ed9f7","weight":0,"created_at":1490116516932,"upstream_id":"413ea7ee-2740-445d-9c2e-d042e2c7a798"}
{"order":"1.2.3.4:8000:1490116516932:196be764-8fbe-4332-a1d4-c192c7cd6bb5","target":"1.2.3.4:8000","id":"196be764-8fbe-4332-a1d4-c192c7cd6bb5","weight":20,"created_at":1490116516932,"upstream_id":"413ea7ee-2740-445d-9c2e-d042e2c7a798"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment