Home >Web Front-end >JS Tutorial >How Can I Sort an Array of Objects by Multiple Fields in JavaScript?

How Can I Sort an Array of Objects by Multiple Fields in JavaScript?

DDD
DDDOriginal
2024-12-17 05:53:24365browse

How Can I Sort an Array of Objects by Multiple Fields in JavaScript?

Sorting Arrays of Objects by Multiple Fields

In the original question, sorting an array of objects by a single field was solved efficiently. However, how can we apply a sort on multiple fields?

To sort an array of objects by multiple fields, we can use a chained sorting approach. This involves sorting by the first field and then, if the values are equal, continuing to sort by the second field. If necessary, this process can be repeated for more fields.

Consider the following example where we want to sort an array of homes by city (ascending) and then price (descending):

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":"6",
     "city":"Dallas",
     "state":"TX",
     "zip":"75000",
     "price":"556699"},
    {"h_id":"5",
     "city":"New York",
     "state":"NY",
     "zip":"00010",
     "price":"962500"}
    ];

To sort this array, we can use the following JavaScript code:

homes.sort(function (a, b) {
    return a.city.localeCompare(b.city) || b.price - a.price;
});

Here's how this code works:

  1. The sort function takes a comparison function as its argument. This function compares two elements in the array.
  2. In our comparison function, we first compare the cities of the two homes. If they are equal, we move on to compare their prices.
  3. The localeCompare function compares two strings and takes into account the locale of the browser. This ensures that the sorting is consistent across different locales.
  4. The || operator is used to perform a logical OR operation. If the city comparison returns a non-zero value, it means that the two homes have different cities and the city comparison should be used for sorting. Otherwise, the price comparison is used.
  5. If the price comparison returns a negative value, it means that the first home has a lower price than the second home. If it returns a positive value, the first home has a higher price than the second home. The sorting algorithm will use this value to determine the order of the homes.

By applying this sorting approach to multiple fields, we can easily sort complex arrays of objects in the desired order.

The above is the detailed content of How Can I Sort an Array of Objects by Multiple Fields in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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