I want to be able to sore a json object array not by a field, but by the most recent post to later post, or I believe it would be ascending order.
My typical approach would be by comparing a field like first or last name with the following:
someArray.sort(function(a, b){
if ( a.someField < b.someField )
return -1;
if ( a.someField > b.someField )
return 1;
return 0;
});
The data set returned is form my api, which returns a json object array from MongoDb.
Thanks for any advice!!
Sample of dataset returned:
[
{
"_id": "5a787d79e2676c6711cfaf99",
"owner_id": "5a787bacae6d9926d313051e",
"__v": 0,
"first_name": "Sarah",
"last_name": "Paul",
"initial": "",
"accepted": true,
},
{
"_id": "5a787d79e2676c6788cfaf99",
"owner_id": "5a787bacae6d9926d313051e",
"__v": 0,
"first_name": "Jim",
"last_name": "Albertson",
"initial": "B",
"accepted": true,
},
{
"_id": "5a787d79e2676c6711cfa089",
"owner_id": "5a787bacae6d9926d313051e",
"__v": 0,
"first_name": "Corry",
"last_name": "Rich",
"initial": "",
"accepted": false,
},
]
Sample of code in my API that returns dataset:
router.route('/getbyowner/:ownerId')
.get(function(req, res) {
Alerts.find({ owner_id: req.params.ownerId }, function(err, alerts) {
if (err)
res.send(err);
res.json(alerts);
})
});