Sorting number of lists in Redis? -
is possible sort number of lists "column" in redis this:
mylist 3 5 6 mylist2 1 6 8 mylist3 4 9 3 (sort first column) mylist2 1 6 8 mylist 3 5 6 mylist3 4 9 3
yes, you'll have prepare data it. 1 way doing using sorted set in you'll keep lists' key names members , scores first member ("column") of these. note you'll have update sorted set whenever make changes relevant lists.
in example, sorted set should created with:
zadd lists_index 3 mylist 1 mylist2 4 mylist3
to sorted list of lists, zrangebyscore lists_index -inf +inf
.
update: following op's comment, lua way of tackling this, so:
local t = {} _, k in pairs(keys) local s = redis.call('lindex', k, argv[1]) t[#t+1] = { s, k } end table.sort(t, function (a, b) return (a[1] < b[1]) end) local r = {} i, v in pairs(t) r[#r+1] = v[2] end return r
and run way:
$ redis-cli --eval sort_by_column.lua mylist mylist2 mylist3 , 0 1) "mylist2" 2) "mylist" 3) "mylist3"
note, however, using approach require more resources whenever invoke sort operation (as opposed the sorted set maintained continuously).
Comments
Post a Comment