Multi-sorting多维数组

时间:2021-03-28 21:28:22
var availableTags = [
    {value:"fruit",desc:"fruit",groupId:2,userId:4},
    {value:"aGan",desc:"normal user",groupId:4,userId:5},
    {value:"father's home ",desc:"normal user",groupId:2,userId:4}     

  ].sort(function(a, b) {  return a.groupId > b.groupId; });

This sorts by groupId field, but how do I to sort by groupId and value?

这是按群域排序,但我如何按群域和值排序呢?

3 个解决方案

#1


7  

Change the return statement to

将return语句更改为

return a.groupId > b.groupId || (a.groupId == b.groupId && a.value > b.value);

#2


2  

How about

如何

.sort(function (a, b) {
    var firstGroupId = a.groupId;
    var secondGroupId = b.groupId;

    return (firstGroupId === secondGroupId) ? a.value > b.value : firstGroupId > secondGroupId;
});

#3


0  

Javascript Multi-Criteria Sort

Javascript多准则排序

If you want to sort by groupId and value, you can use the sort function that I've pasted below (JsFiddle: http://jsfiddle.net/oahxg4u3/6/). This sort function can also be used to sort by n-values, or by a single value.

如果您想按groupId和value排序,可以使用我粘贴在下面的sort函数(JsFiddle: http://jsfiddle.net/oahxg4u3/6/)。这个排序函数还可以用于按n值或单个值排序。

Define:

定义:

function sortByCriteria(data, criteria) {
    return data.sort(function (a, b) {

        var i, iLen, aChain, bChain;

        i = 0;
        iLen = criteria.length;
        for (i; i < iLen; i++) {        
            aChain += a[criteria[i]];
            bChain += b[criteria[i]];
        }

        return aChain.localeCompare(bChain);
    });
}

Invoke:

调用:

var data = [
    {value:"fruit", desc:"fruit", groupId:2, userId:4},
    {value:"aGan", desc:"normal user", groupId:4, userId:5},
    {value:"father's home ", desc:"normal user", groupId:2, userId:4}
];
var criteria = ["groupId", "value"];

sortByCriteria(data, criteria);

#1


7  

Change the return statement to

将return语句更改为

return a.groupId > b.groupId || (a.groupId == b.groupId && a.value > b.value);

#2


2  

How about

如何

.sort(function (a, b) {
    var firstGroupId = a.groupId;
    var secondGroupId = b.groupId;

    return (firstGroupId === secondGroupId) ? a.value > b.value : firstGroupId > secondGroupId;
});

#3


0  

Javascript Multi-Criteria Sort

Javascript多准则排序

If you want to sort by groupId and value, you can use the sort function that I've pasted below (JsFiddle: http://jsfiddle.net/oahxg4u3/6/). This sort function can also be used to sort by n-values, or by a single value.

如果您想按groupId和value排序,可以使用我粘贴在下面的sort函数(JsFiddle: http://jsfiddle.net/oahxg4u3/6/)。这个排序函数还可以用于按n值或单个值排序。

Define:

定义:

function sortByCriteria(data, criteria) {
    return data.sort(function (a, b) {

        var i, iLen, aChain, bChain;

        i = 0;
        iLen = criteria.length;
        for (i; i < iLen; i++) {        
            aChain += a[criteria[i]];
            bChain += b[criteria[i]];
        }

        return aChain.localeCompare(bChain);
    });
}

Invoke:

调用:

var data = [
    {value:"fruit", desc:"fruit", groupId:2, userId:4},
    {value:"aGan", desc:"normal user", groupId:4, userId:5},
    {value:"father's home ", desc:"normal user", groupId:2, userId:4}
];
var criteria = ["groupId", "value"];

sortByCriteria(data, criteria);