Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

I have got the following objects using AJAX and stored them in an array:

var homes = [

    {

        "h_id": "3",

        "city": "Dallas",

        "state": "TX",

        "zip": "75201",

        "price": "162500"

    }, {

        "h_id": "4",

        "city": "Bevery Hills",

        "state": "CA",

        "zip": "90210",

        "price": "319250"

    }, {

        "h_id": "5",

        "city": "New York",

        "state": "NY",

        "zip": "00010",

        "price": "962500"

    }

];

How do I create a function to sort the objects by the price property in ascending or descending order using JavaScript only?

1 Answer

0 votes
by (13.1k points)

Sorting in ascending order:

homes.sort((a,b) => parseFLoat(a.price) - parseFloat(b.price));

Sorting in descending order:

homes.sort((a,b)=> parseFloat(b.price) - parseFloat(a.price));

Want to be a full stack developer? Check out the full stack developer course from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers
asked Feb 18, 2021 in Java by Harsh (1.5k points)

Browse Categories

...