如何获得按“值"降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?

How can I get a map/reduce result which is sorted within descending order of the quot;valuequot; value?if also using list function can achieve that?(如何获得按“值降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?)
本文介绍了如何获得按“值"降序排序的 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)
} 

这篇关于如何获得按“值"降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)