根据嵌套的json值对lua表进行排序

时间:2017-04-20 15:38:45

标签: json lua

我们在redis中有一个键值对,包含一个带有JSON对象的键作为具有各种信息的值;

"node:service:i-01fe0d69c343734" : 
"{\"port\":\"32781\",
\"version\":\"3.0.2\",
\"host-instance-id\":\"i-01fe0d69c2243b366\",
\"last-checkin\":\"1492702508\",
\"addr\":\"10.0.0.0\",
\"host-instance-type\":\"m3.large\"}"

是否可以根据值的last-checkin时间对表格进行排序?

1 个答案:

答案 0 :(得分:1)

以下是我的问题解决方案,使用快速排序算法,然后对输入进行一些修正(据我理解):

 -----------------------------------------------------
local   json = require("json")

function quicksort(t, sortname, start, endi)
  start, endi = start or 1, endi or #t
  sortname = sortname or 1
  if(endi - start < 1) then return t end
  local pivot = start
  for i = start + 1, endi do
    if  t[i][sortname] <= t[pivot][sortname] then
      local temp = t[pivot + 1]
      t[pivot + 1] = t[pivot]
      if(i == pivot + 1) then
        t[pivot] = temp
      else
        t[pivot] = t[i]
        t[i] = temp
      end
      pivot = pivot + 1
    end
  end
  t = quicksort(t, sortname, start, pivot - 1)
  return quicksort(t, sortname, pivot + 1, endi)
end
---------------------------------------------------------

-- I manually added  delimeter ","  
-- and name "node:service..." must be different
 str = [[
 {
 "node:service:i-01fe0d69c343731" : 
"{\"port\":\"32781\",
\"version\":\"3.0.2\",
\"host-instance-id\":\"i-01fe0d69c2243b366\",
\"last-checkin\":\"1492702506\",
\"addr\":\"10.0.0.0\",
\"host-instance-type\":\"m3.large\"}"
,
 "node:service:i-01fe0d69c343732" : 
"{\"port\":\"32781\",
\"version\":\"3.0.2\",
\"host-instance-id\":\"i-01fe0d69c2243b366\",
\"last-checkin\":\"1492702508\",
\"addr\":\"10.0.0.0\",
\"host-instance-type\":\"m3.large\"}"
,
 "node:service:i-01fe0d69c343733" : 
"{\"port\":\"32781\",
\"version\":\"3.0.2\",
\"host-instance-id\":\"i-01fe0d69c2243b366\",
\"last-checkin\":\"1492702507\",
\"addr\":\"10.0.0.0\",
\"host-instance-type\":\"m3.large\"}"
,
 "node:service:i-01fe0d69c343734" : 
"{\"port\":\"32781\",
\"version\":\"3.0.2\",
\"host-instance-id\":\"i-01fe0d69c2243b366\",
\"last-checkin\":\"1492702501\",
\"addr\":\"10.0.0.0\",
\"host-instance-type\":\"m3.large\"}"
}
 ]]

 -- remove unnecessary \

 str = str:gsub('"{','{'):gsub('}"','}'):gsub('\\"','"')

local t_res= json.decode(str)
-- prepare table before sorting
local t_indexed = {}
for k,v in pairs(t_res) do
    v["node-service"] = k
     t_indexed[#t_indexed+1] = v
end
-- algoritm quicksort realised only for indexed table
local t_sort= quicksort(t_indexed, "last-checkin")

for k,v in pairs(t_sort) do
     print( k , v["node-service"] , v["port"], v["version"], v["host-instance-id"], v["last-checkin"] , v["addr"], v["host-instance-type"]  )
end

控制台:

1   node:service:i-01fe0d69c343734  32781   3.0.2   i-01fe0d69c2243b366 1492702501  10.0.0.0    m3.large
2   node:service:i-01fe0d69c343731  32781   3.0.2   i-01fe0d69c2243b366 1492702506  10.0.0.0    m3.large
3   node:service:i-01fe0d69c343733  32781   3.0.2   i-01fe0d69c2243b366 1492702507  10.0.0.0    m3.large
4   node:service:i-01fe0d69c343732  32781   3.0.2   i-01fe0d69c2243b366 1492702508  10.0.0.0    m3.large