在Lua中对一个关联数组的数组进行排序。

我有一个具有以下结构的数组:

[{"value":{"date":"2015-12-27T16:02:38.109Z", "read":"no"}, "key":"56800b9e9669ef7312d94f4c"}, {"value":{"date":"2015-12-30T13:01:30.580Z", "read":"no"}, "key":"5683d5aaec6a8c2428ca1011"},...]

我想按降序按日期对此数组进行排序,并进行分页。

例如我尝试使用以下方式,其中wholeList是数组的名称,如果要从索引1到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

但它不起作用。我也尝试使用pairs,但它不起作用。

我得到的错误代码为:

 { 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 }

注:如果我只返回'wholeList',我会得到整个数组,因此我假设错误是在使用table.sort时发生的。 我错过了什么?

编辑:我尝试了另一种方法:在aerospike udf中丢弃和修剪列表

点赞