根据其属性值对JSON数组进行排序

时间:2014-07-01 05:45:02

标签: javascript arrays json node.js sorting

这是我得到的JSON数组:

    [
        {
            "Name" : "Sachin",
            "Age"  : "41",
            "Team" : "Mumbai"
        },
        {
            "Name" : "Dravid",
            "Age"  : "42",
            "Team" : "Rajasthan"
        },
        {
            "Name" : "Yuvraj",
            "Age"  : "31",
            "Team" : "Bangalore"
        }
    ]

但是我需要通过“Age”属性对这个JSON数组desc进行排序。 我想要的JSON数组应如下所示:

 [
    {
        "Name" : "Dravid",
        "Age"  : "42",
        "Team" : "Rajasthan"
    },
    {
        "Name" : "Sachin",
        "Age"  : "41",
        "Team" : "Mumbai"
    },
    {
        "Name" : "Yuvraj",
        "Age"  : "31",
        "Team" : "Bangalore"
    }
]

如何实现这一目标?

5 个答案:

答案 0 :(得分:5)

var a = [
    {
        "Name" : "Sachin",
        "Age"  : "41",
        "Team" : "Mumbai"
    },
    {
        "Name" : "Dravid",
        "Age"  : "42",
        "Team" : "Rajasthan"
    },
    {
        "Name" : "Yuvraj",
        "Age"  : "31",
        "Team" : "Bangalore"
    }
];

   a.sort(function(x,y){return y["Age"]-x["Age"]});
   console.log(a);

答案 1 :(得分:2)

使用以下通用函数predicateBy按所需字段对数据进行排序

var data=[
            {
                "Name" : "Sachin",
                "Age"  : "41",
                "Team" : "Mumbai"
            },
            {
                "Name" : "Dravid",
                "Age"  : "42",
                "Team" : "Rajasthan"
            },
            {
                "Name" : "Yuvraj",
                "Age"  : "31",
                "Team" : "Bangalore"
            }
        ]    

    function predicatBy(prop){
           return function(a,b){
              if( a[prop] > b[prop]){
                  return 1;
              }else if( a[prop] < b[prop] ){
                  return -1;
              }
              return 0;
           }
        }

        //Usage
        data.sort( predicatBy("age") );
        console.log(data);

答案 2 :(得分:1)

var _ = require('underscore');

var data = ...your data...
console.log(_.sortBy(data, 'Age').reverse());

答案 3 :(得分:1)

试试这个:

var arr =   [
        {
            "Name" : "Sachin",
            "Age"  : "41",
            "Team" : "Mumbai"
        },
        {
            "Name" : "Dravid",
            "Age"  : "42",
            "Team" : "Rajasthan"
        },
        {
            "Name" : "Yuvraj",
            "Age"  : "31",
            "Team" : "Bangalore"
        }
    ]

var prop = "Age"

arr.sort(function(a,b){
    var cmp = -1
    if (a.hasOwnProperty(prop) && b.hasOwnProperty(prop)){
        var a_prop_value = parseFloat(a[prop])
        var b_prop_value = parseFloat(b[prop])
        if (isNaN(a_prop_value) || isNaN(b_prop_value)){
            //string comp
            cmp = a[prop].localeCompare(b[prop])
        }else{
            cmp = a_prop_value - b_prop_value > 0? 1 : a_prop_value - b_prop_value ==0? 0:-1
        }
    }
    return cmp;
});    

答案 4 :(得分:1)

天真的答案是使用Array.prototype.sort([compareFunction])。阻碍的方式:

function compareAgeProperty(a,b) {
    return (parseInt(a.Age) < parseInt(b.Age)) ? -1 : 1; 
}

var arr = [/* your array's data here */];

arr.sort(compareAgeProperty);

我建议你看看: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

您的Age属性是一个字符串,因此比较为字符串,表示'80'&lt;'9'=== true。解析该属性应该可以解决这个问题。