I fetched data from mongoDB and sent it to client side, JSON returned is structured like this:
{ "0" : { "_id" : { "$oid" : "57d129bfaa1a7567d9d" }, "id" : "155edc1698efc18e", "history_id" : "41752", "messages" : [ { "id" : "155edc1698efc18e", "snippet" : "Mari567567rnar | IT U756567ive travel experience T: +35667575756756| T: +7567560 F: +3567756792 | E: ad75675travel.com Ulix doo | Miramarsk765657 HR–105675 Zagreb ----------", "labels" : [ "INBOX", "IMPORTANT", "CATEGORY_PERSONAL", "Label_15", "Label_7" ], "date" : "1468572702000", "history_id" : "41752",
I parsed it using var objects = JSON.parse(response);
When I log it, it looks like this:
It is basically an object of objects. It is acting like array but it is not array and I can't use array methods such as sort
.
This is how my inner objects looks like.
{_id:object, history_id:string, id:string, messages:array}
I am about to convert history_id value into int and sort these objects by that value.
I am looking for the best method to sort these objects by that value. I went with .sort
method but it is array method and it is not defined for object type. I would convert the main object into array and call that method. Or else, if there is a way to sort these objects in a more elegant way, without converting it into array. Which method is more practical and how to implement it properly?