2

This is the JSON Array what Iam getting:

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

But I need to sort this JSON array desc by the "Age" attribute. My desired JSON Array should be like below:

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

How to achieve this?

thevan
  • 10,052
  • 53
  • 137
  • 202

5 Answers5

6
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);
azero0
  • 2,220
  • 3
  • 20
  • 31
2

Use the following generic function predicateBy to sort your data by the desired field

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);
abhinsit
  • 3,214
  • 4
  • 21
  • 26
1
var _ = require('underscore');

var data = ...your data...
console.log(_.sortBy(data, 'Age').reverse());
7zark7
  • 10,015
  • 5
  • 39
  • 54
1

Try this:

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;
});    
xecgr
  • 5,095
  • 3
  • 19
  • 28
  • Note that [*localeCompare*](http://ecma-international.org/ecma-262/5.1/#sec-15.5.4.9) is implementation dependant and may not produce consistent results. – RobG Jul 01 '14 at 05:57
  • its giving correct array structure when we look for Name, but not giving correct solution for Age attribute. – thevan Jul 01 '14 at 06:39
  • i did a read mistake, you are wright @thevan, have a look to my last edit – xecgr Jul 01 '14 at 06:54
  • Ok, now you can sort by any of the JSON props, only changing the prop variable – xecgr Jul 01 '14 at 07:11
1

The naive answer would be to use Array.prototype.sort([compareFunction]). Something in the way of:

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

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

arr.sort(compareAgeProperty);

I'd recommend you take a look at: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Your Age property is a string, and thus compared as a string, meaning '80'<'9'===true. Parsing the property should solve this issue.

javinor
  • 674
  • 4
  • 8