问题
I have an array that have this structure :
[{"value":{"date":"2015-12-27T16:02:38.109Z", "read":"no"}, "key":"56800b9e9669ef7312d94f4c"}, {"value":{"date":"2015-12-30T13:01:30.580Z", "read":"no"}, "key":"5683d5aaec6a8c2428ca1011"},...]
I want to sort this array by date in descending order with pagination.
I tried this for example where wholeList is the name of the array and if want to return from indice 1 to 9:
local function compare(a,b)
return os.date(b["date"]) < os.date(a["date"])
end
function get(rec, ldtBinName, from, to)
if aerospike:exists(rec) then
local count = llist.size(rec, ldtBinName);
if count > 0 then
wholeList = llist.scan(rec, ldtBinName);
table.sort(wholeList, compare);
return table.unpack(wholeList , from , to);
--end if count > 0
else
return {};
--rec['count'] = 0;
--return empty array
end
--end else if count > 0
--end if aerospike exists
else
return {};--end return empty array
end--end else aerospike exists
end
But it didn't work. I also tried to use pairs , but it didn't work.
The error code I got is :
{ code: 100,
message: '/opt/aerospike/usr/udf/lua/timelines.lua:49: bad argument #1 to \'sort\' (table expected, got userdata)',
func: 'as_command_parse_udf_error', file: 'src/main/aerospike/as_command.c', line: 822 }
Note : if I just return 'wholeList' I got the whole array so , I assume that the error is coming when I use table.sort What am I missing?
Edit : I try another method : drop and trim a list in aerospike udf
来源:https://stackoverflow.com/questions/34564418/sort-an-array-of-associatives-arrays-in-lua