Problem trying to sort a JSON object. Basically, people can add products in any random order to our order form, but the order that it shows in the summary should be the way we want them to be positioned (and not the order they choose), so therefore I need to sort by 'id' (or we will sort the "pos" field later)
Essentially, I need to sort by id in ascending order. 1,2,103 instead of 2,103,1
I seem to have problems, because the index into individual objects is numbers (or just that they are there ...).
I need to do something line by line array.sort (function (a, b) {return a.id-b.id}); but I assume that this does not work, because 1, its not an array (its object) and 2, it has these annoying indexes (which I need for another part of my code) ...
Any ideas ????
var products = {
"2": {
"id": "2",
"price": "119",
"quantity": "1",
"thumb": "img\/store\/comp-08n.png"
},
"103": {
"id": "103",
"price": "109",
"quantity": "1",
"thumb": "img\/store\/basketballhoop.png"
},
"1": {
"id": "1",
"price": "309",
"quantity": "1",
"thumb": "img\/store\/comp-08.png"
}
};
source
share