如何通过两个键来订购JSON对象?

时间:2022-11-23 08:58:44

I have a JSON object that I want to sort by one key first, then by a second key similar to ordering by two columns in SQL. Here is a sample of the JSON I would have:

我有一个JSON对象,我想先按一个键排序,然后再按第二个键排序,类似于用SQL中的两个列排序。下面是JSON的一个示例:

{
   "GROUPID":3169675,
   "LASTNAME":"Chantry"
}

I would like to order all the results by the GROUPID and then by LASTNAME. I've used the JSON sort function to sort by one key but not multiple.

我想订购所有的结果由GROUPID,然后由LASTNAME。我使用JSON排序函数按一个键排序,但不是按多个键排序。

Any help would be great.

任何帮助都是伟大的。

2 个解决方案

#1


9  

Assuming you have an array of objects:

假设你有一个对象数组:

var data = [
    { "GROUPID":3169675, "LASTNAME":"Chantry" },
    { "GROUPID":3169612, "LASTNAME":"Doe" },
    ...
];

You can use a custom comparator to do the sorting. To sort first by GROUPID, and then by LASTNAME, the logic to compare two objects would be:

可以使用自定义比较器进行排序。先用GROUPID排序,然后用LASTNAME,比较两个对象的逻辑是:

if GROUPID of first is smaller than second
    return -1;
else if GROUPID of first is larger than second
    return 1;
else if LASTNAME of first is smaller than second
    return -1;
else if LASTNAME of first is larger than second
    return 1;
else
    return 0;

To sort the object array, use the above algorithm and call the sort method on the array. After sorting is done, data should have the elements in required sorted order.

要对对象数组进行排序,请使用上述算法并调用数组上的排序方法。排序完成后,数据应该具有所需的排序顺序的元素。

data.sort(function(a, b) {
    // compare a and b here using the above algorithm
});

Here's another very similar question I answered recently. It's regarding sorting on multiple columns using jQuery, but you can strip out the jQuery part easily. It presents some customizable approaches which can extend to multiple columns.

这是我最近回答的另一个非常相似的问题。它是关于使用jQuery对多个列进行排序,但是您可以轻松地去掉jQuery部分。它提供了一些可定制的方法,可以扩展到多个列。

#2


37  

Here is a generic way to sort an array of objects, with multiple columns:

这里是一种对对象数组进行排序的通用方法,有多个列:

var arr = [
    { id:5, name:"Name3" },
    { id:4, name:"Name1" },
    { id:6, name:"Name2" },
    { id:3, name:"Name2" }
],

// generic comparison function
cmp = function(x, y){
    return x > y ? 1 : x < y ? -1 : 0; 
};

//sort name ascending then id descending
arr.sort(function(a, b){
    //note the minus before -cmp, for descending order
    return cmp( 
        [cmp(a.name, b.name), -cmp(a.id, b.id)], 
        [cmp(b.name, a.name), -cmp(b.id, a.id)]
    );
});

To add other columns to sort on, you can add other items in the array comparison.

要添加其他列进行排序,可以在数组比较中添加其他项。

arr.sort(function(a, b){
    return cmp( 
        [cmp(a.name, b.name), -cmp(a.id, b.id), cmp(a.other, b.other), ...], 
        [cmp(b.name, a.name), -cmp(b.id, a.id), cmp(b.other, a.other), ...]
    );
});

EDIT: per @PhilipZ comment below, the array comparison in JS convert them in strings separated by comas.

编辑:根据下面的@PhilipZ注释,JS中的数组比较将它们转换成由comas分隔的字符串。

#1


9  

Assuming you have an array of objects:

假设你有一个对象数组:

var data = [
    { "GROUPID":3169675, "LASTNAME":"Chantry" },
    { "GROUPID":3169612, "LASTNAME":"Doe" },
    ...
];

You can use a custom comparator to do the sorting. To sort first by GROUPID, and then by LASTNAME, the logic to compare two objects would be:

可以使用自定义比较器进行排序。先用GROUPID排序,然后用LASTNAME,比较两个对象的逻辑是:

if GROUPID of first is smaller than second
    return -1;
else if GROUPID of first is larger than second
    return 1;
else if LASTNAME of first is smaller than second
    return -1;
else if LASTNAME of first is larger than second
    return 1;
else
    return 0;

To sort the object array, use the above algorithm and call the sort method on the array. After sorting is done, data should have the elements in required sorted order.

要对对象数组进行排序,请使用上述算法并调用数组上的排序方法。排序完成后,数据应该具有所需的排序顺序的元素。

data.sort(function(a, b) {
    // compare a and b here using the above algorithm
});

Here's another very similar question I answered recently. It's regarding sorting on multiple columns using jQuery, but you can strip out the jQuery part easily. It presents some customizable approaches which can extend to multiple columns.

这是我最近回答的另一个非常相似的问题。它是关于使用jQuery对多个列进行排序,但是您可以轻松地去掉jQuery部分。它提供了一些可定制的方法,可以扩展到多个列。

#2


37  

Here is a generic way to sort an array of objects, with multiple columns:

这里是一种对对象数组进行排序的通用方法,有多个列:

var arr = [
    { id:5, name:"Name3" },
    { id:4, name:"Name1" },
    { id:6, name:"Name2" },
    { id:3, name:"Name2" }
],

// generic comparison function
cmp = function(x, y){
    return x > y ? 1 : x < y ? -1 : 0; 
};

//sort name ascending then id descending
arr.sort(function(a, b){
    //note the minus before -cmp, for descending order
    return cmp( 
        [cmp(a.name, b.name), -cmp(a.id, b.id)], 
        [cmp(b.name, a.name), -cmp(b.id, a.id)]
    );
});

To add other columns to sort on, you can add other items in the array comparison.

要添加其他列进行排序,可以在数组比较中添加其他项。

arr.sort(function(a, b){
    return cmp( 
        [cmp(a.name, b.name), -cmp(a.id, b.id), cmp(a.other, b.other), ...], 
        [cmp(b.name, a.name), -cmp(b.id, a.id), cmp(b.other, a.other), ...]
    );
});

EDIT: per @PhilipZ comment below, the array comparison in JS convert them in strings separated by comas.

编辑:根据下面的@PhilipZ注释,JS中的数组比较将它们转换成由comas分隔的字符串。