How to sort JSON by a single integer field?
javascript, json, node.js, sorting
Solution
At first, your need valid JSON, like that:
var unsorted = {
"items": [
{
"title": "Book",
"order": 0
},
{
"title": "Movie",
"order": 9
},
{
"title": "Cheese",
"order": 2
}
]
};
Afterwards you can easily sort the `items` and store them in a list.
var sorted = unsorted.items.sort(function(a, b) {return a.order - b.order});
Problem
I have the following JSON: ``` { title: 'title', ..., order: 0 }, { ..., order: 9 }, { ..., order: 2 } ``` ... the JSON includes many fields, how can I sort them based on the order field? I was looking for something build into nodejs but I couldn't find anything that might be useful for that case.