-1
[
{
    "_id": "560be5d1b28f247c38a4df98",
    "name": "sara",
    "location": "hjk",
    "price": 2000,
    "rating": 4.2,
    "__v": 0,
    "description":"beautiful"

  },
{
    "_id": "560be5d1b28f247c20a4df98",
    "name": "zara",
    "location": "hjk",
    "price": 1000,
    "rating": 3.2,
    "__v": 0,
    "description":"amazing"
  }
]

How to sort the following array of JavaScript objects created by using post method according to its price?

RayOfHope
  • 15
  • 2
  • 7
  • What does this have to do with node.js? –  Oct 02 '15 at 04:43
  • 2
    Possible duplicate of [Sort array of objects by string property value in JavaScript](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) –  Oct 02 '15 at 04:44

1 Answers1

1

First load your json string into an object using JSON.parse. Next, once you have the object loaded into JavaScript, you can use the built-in sort method to loop through the elements of the array as many times as needed to sort them based on some logic which returns either a positive number, negative number, or 0 indicating which element should go in front of which or if they're of equal sorting position.

var o=[
{
    "_id": "560be5d1b28f247c38a4df98",
    "name": "sara",
    "location": "hjk",
    "price": 2000,
    "rating": 4.2,
    "__v": 0,
    "description":"beautiful"

  },
{
    "_id": "560be5d1b28f247c20a4df98",
    "name": "zara",
    "location": "hjk",
    "price": 1000,
    "rating": 3.2,
    "__v": 0,
    "description":"amazing"
  }
];
var ascending=true;//change to false for descending
o.sort(function(a,b) {
        return (a.price - b.price)*(ascending?1:-1);
});
//variable "o" now contains our sorted object.
var json = JSON.stringify(o);//convert back to a string
//now do something with either the "o" object or "json" string
Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • How to declare the name of the array? like var o. The data is already stored. – RayOfHope Oct 02 '15 at 05:14
  • As per the beginning of my post, use JSON.parse. For example, you might do something like `var o=JSON.parse(yourJSONString);` – Ultimater Oct 02 '15 at 05:17
  • There's a difference between JSON and JavaScript objects. JSON is a standardized JavaScript Object Notation used for exchanging data between programming languages, and is stored as a string. JavaScript objects, on the other hand, are objects stored in the JavaScript engine as object rather than being stored as strings. JSON is stored as a string, while, to be useful to JavaScript, you first need to parse the JSON string into an object/array so JavaScript can use the data. – Ultimater Oct 02 '15 at 05:29
  • Had there been more than 2 elements in the array. How would I sort them? Like what should be passed in a and b? – RayOfHope Oct 02 '15 at 05:47
  • The Array `sort()` method loops through all the elements of an array, no matter how big it is, by comparing two elements at a time. It will loop as many times as required until the entire array is sorted by comparing two items with each other at a time, swapping the two items with each other as needed until the entire array is reordered. – Ultimater Oct 02 '15 at 05:52
  • What will be 'yourJSONString' ? – RayOfHope Oct 02 '15 at 06:07
  • I am doing this in node js. Will it still be same? – RayOfHope Oct 02 '15 at 06:27
  • I am demonstrating my code, which uses `JSON.parse`, `JSON.stringify`, and `sort` with client-side JavaScript so you'll understand what my code does so you can use it in Node.js on the server. Array sorting is a core JavaScript built-in feature, and since node.js extends the core functionality of JavaScript as per the Google V8 JavaScript engine, my code will work both client-side and server-side. – Ultimater Oct 02 '15 at 06:43