如何获得按“值"降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?
我有这样的视图地图和减少:地图:
I have view map and reduce like this: Map:
function(doc) {
if(doc.type){
var usersLength = doc.users.length;
for (var i = 0; i < usersLength ; i++) {
emit([doc.users[i].userid,doc.Service.ownId], 1);
}
}
}
减少:
function(keys, values, rereduce)
{
return sum(values);
}
然后像这样调用视图:_design/aaa/_view/getUserId?group=true&group_level=2&startkey=["111111"]&endkey=["111111",{}]代码>
我可以得到下面的结果集:
I can get the result set below:
{"rows":[
{"key":["111111",""],"value":7},
{"key":["111111","FFFF26"],"value":1},
{"key":["111111","FFFF44"],"value":7},
{"key":["111111","FFFF34"],"value":2}
]}
但是,我想让上面的结果集按值"值的降序排序.如果还使用列表功能可以实现吗?预期结果如下:
However, I want to get the result set above sorted within descending order of the "value" value. if also using list function can achieve that? the following would be expected results:
{"rows":[
{"key":["111111",""],"value":7},
{"key":["111111","FFFF44"],"value":7},
{"key":["111111","FFFF34"],"value":2},
{"key":["111111","FFFF26"],"value":1}
]}
推荐答案
如果要对结果集进行排序,则必须按值降序排序,可以使用比较器回调进行排序:
If you want to sort the result set you have to one that is sorted by value descending you can sort with a comparator callback:
// ES6 Syntax
let originalResult = {
"rows":[
{"key":["111111",""],"value":7},
{"key":["111111","FFFF26"],"value":1},
{"key":["111111","FFFF44"],"value":7},
{"key":["111111","FFFF34"],"value":2}
]}
let newResult = {
rows: originalResult.rows.sort((a, b) => b.value - a.value)
}
相关文章