用于对对象数组进行排序的简单函数

时间:2011-11-17 22:10:09

标签: javascript sorting

我想创建一个(非匿名)函数,按键name按字母顺序对对象数组进行排序。我只编写直接的JavaScript代码,所以框架至少对我没用。

var people = [
    {'name': 'a75', 'item1': false, 'item2': false},
    {'name': 'z32', 'item1': true,  'item2': false},
    {'name': 'e77', 'item1': false, 'item2': false}
];

11 个答案:

答案 0 :(得分:107)

这个怎么样?

var people = [
{
    name: 'a75',
    item1: false,
    item2: false
},
{
    name: 'z32',
    item1: true,
    item2: false
},
{
    name: 'e77',
    item1: false,
    item2: false
}];

function sortByKey(array, key) {
    return array.sort(function(a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
}

people = sortByKey(people, 'name');

这允许您指定要对数组进行排序的键,以便您不限于硬编码名称排序。它将对所有共享属性的对象数组进行排序,这些属性用作键。我相信这就是你要找的东西?

这是一个jsFiddle:http://jsfiddle.net/6Dgbu/

答案 1 :(得分:26)

您可以使用[...]函数对数组.sort)进行排序:

var people = [
    {'name': 'a75', 'item1': false, 'item2': false},
    {'name': 'z32', 'item1': true,  'item2': false},
    {'name': 'e77', 'item1': false, 'item2': false},
];

var sorted = people.sort(function IHaveAName(a, b) { // non-anonymous as you ordered...
    return b.name < a.name ?  1 // if b should come earlier, push a to end
         : b.name > a.name ? -1 // if b should come later, push a to begin
         : 0;                   // a and b are equal
});

答案 2 :(得分:8)

这本身不是JSON问题。它是一个javascript数组问题。

试试这个:

people.sort(function(a,b){ 
    var x = a.name < b.name? -1:1; 
    return x; 
});

答案 3 :(得分:1)

summaryType: function () { return "Total:"; }

使用小隔间并使用排序,我们可以做到

答案 4 :(得分:1)

我使用ECMA 6的类似排序问题的解决方案

&#13;
&#13;
var library = [
        {name: 'Steve', course:'WAP', courseID: 'cs452'}, 
        {name: 'Rakesh', course:'WAA', courseID: 'cs545'},
        {name: 'Asad', course:'SWE', courseID: 'cs542'},
];

const sorted_by_name = library.sort( (a,b) => a.name > b.name );

for(let k in sorted_by_name){
    console.log(sorted_by_name[k]);
}
&#13;
&#13;
&#13;

答案 5 :(得分:1)

我使用lambda修改了@Geuis的答案,并首先将其转换为大写:

people.sort((a, b) => a.toLocaleUpperCase() < b.toLocaleUpperCase() ? -1 : 1);

答案 6 :(得分:0)

Array.prototype.sort_by = function(key_func, reverse=false){
    return this.sort( (a, b) => ( key_func(b) - key_func(a) ) * (reverse ? 1 : -1) ) 
}

例如,如果我们有

var arr = [ {id: 0, balls: {red: 8,  blue: 10}},
            {id: 2, balls: {red: 6 , blue: 11}},
            {id: 1, balls: {red: 4 , blue: 15}} ]

arr.sort_by(el => el.id, reverse=true)
/* would result in
[ { id: 2, balls: {red: 6 , blue: 11 }},
  { id: 1, balls: {red: 4 , blue: 15 }},
  { id: 0, balls: {red: 8 , blue: 10 }} ]
*/

arr.sort_by(el => el.balls.red + el.balls.blue)
/* would result in
[ { id: 2, balls: {red: 6 , blue: 11 }},    // red + blue= 17
  { id: 0, balls: {red: 8 , blue: 10 }},    // red + blue= 18
  { id: 1, balls: {red: 4 , blue: 15 }} ]   // red + blue= 19
*/

答案 7 :(得分:0)

这就是我从以前的示例中进行排序的简单方式:

如果我的数组是items

0: {id: 14, auctionID: 76, userID: 1, amount: 39}
1: {id: 1086, auctionID: 76, userID: 1, amount: 55}
2: {id: 1087, auctionID: 76, userID: 1, amount: 55}

我认为只需调用items.sort()就能对其进行排序,但是有两个问题: 1.正在对它们进行字符串排序 2.正在对它们进行排序

这是我修改排序功能的方式:

for(amount in items){
if(item.hasOwnProperty(amount)){

i.sort((a, b) => a.amount - b.amount);
}
}

答案 8 :(得分:0)

var library = [
        {name: 'Steve', course:'WAP', courseID: 'cs452'}, 
        {name: 'Rakesh', course:'WAA', courseID: 'cs545'},
        {name: 'Asad', course:'SWE', courseID: 'cs542'},
];

const sorted_by_name = library.sort( (a,b) => a.name > b.name );

for(let k in sorted_by_name){
    console.log(sorted_by_name[k]);
}

答案 9 :(得分:-1)

var library = [
        {name: 'Steve', course:'WAP', courseID: 'cs452'}, 
        {name: 'Rakesh', course:'WAA', courseID: 'cs545'},
        {name: 'Asad', course:'SWE', courseID: 'cs542'},
];

const sorted_by_name = library.sort( (a,b) => a.name > b.name );

for(let k in sorted_by_name){
    console.log(sorted_by_name[k]);
}

答案 10 :(得分:-2)

var people = 
[{"name": 'a75',"item1": "false","item2":"false"}, 
{"name": 'z32',"item1": "true","item2":  "false"}, 
{"name": 'e77',"item1": "false","item2": "false"}]; 

function mycomparator(a,b) {   return parseInt(a.name) - parseInt(b.name);  } 
people.sort(mycomparator); 

这可能是某些事情(或者正如我们常说的那样,这应该有效)。