Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Sorting an array of objects by property values

I've 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?

Answer*

Draft saved
Draft discarded
Cancel
1
  • I like this answer a lot because of sortBy's concise syntax. Simple to use –even with nested fields– while maintaining great code-readability. Thank you! Commented May 31, 2020 at 16:15

lang-js

AltStyle によって変換されたページ (->オリジナル) /