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?

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

A better name would be sortBy as the predicate is the passed property name. ;-)
1
var _ = require('underscore');

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

Comments

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.

1 Comment

yes.. its giving desired solution when I change the < symbol to > symbol.. thank you..
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;
});    

4 Comments

Note that localeCompare is implementation dependant and may not produce consistent results.
its giving correct array structure when we look for Name, but not giving correct solution for Age attribute.
i did a read mistake, you are wright @thevan, have a look to my last edit
Ok, now you can sort by any of the JSON props, only changing the prop variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.