Home > Web Front-end > JS Tutorial > How to Sort an Array of JavaScript Objects by a Specific Property Value (e.g., Price)?

How to Sort an Array of JavaScript Objects by a Specific Property Value (e.g., Price)?

Barbara Streisand
Release: 2024-12-24 00:32:09
Original
395 people have browsed it

How to Sort an Array of JavaScript Objects by a Specific Property Value (e.g., Price)?

Sorting Objects by Property Values in JavaScript

When handling arrays of objects, sorting them by specific property values can be essential for managing and visualizing data. Let's explore how to achieve this sorting functionality using JavaScript.

Question:

Given an array of objects representing home data, how do we create a function to sort them by their "price" property in either ascending or descending order?

Objects Array:

Consider the following homes 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"
    }
];
Copy after login

Sorting Functions:

To sort the homes array, we can utilize the built-in sort() method. This method expects a comparison function as its argument. The comparison function takes two elements as input and returns a negative number if the first element should come before the second, a positive number if the second element should come before the first, or zero if the elements are considered equal.

Ascending Order:

To sort the homes in ascending order by price, we define a comparison function that subtracts the price of the first element from the price of the second element and compares the difference. If the difference is negative, it means the first element's price is less than the second's price, and the first element should come before the second.

homes.sort(function(a, b) {
    return parseFloat(a.price) - parseFloat(b.price);
});
Copy after login

Or in ES6 syntax:

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

Descending Order:

For descending order, we can reverse the comparison function by subtracting the price of the second element from the price of the first element.

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

Result:

After sorting, the homes array will be sorted based on the specified order. You can print the modified array to verify the sorting.

The above is the detailed content of How to Sort an Array of JavaScript Objects by a Specific Property Value (e.g., Price)?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template