0

I have some id's stored in array locationIds that is already sorted in a way I want it. But with the below codes, I am getting the response in sails default sorted order.

var locationIds = []; // pushing id's in it
Locations.find({ id: locationIds }).paginate({ page: page, limit: limit }).exec(function(err, sortedLocations) {
  // need the response in the same order as in locationIds
});

Is there any way that I can disable this default sorting or any other possibilities? Need help with this, thanks in advance.

  • 3
    When you doesn't specify any order in MongoDB you have no guarantee in which order you will get records. You should get it in **natural order** but you cant be 100% sure. See [How does MongoDB sort records when no sort order is specified?](http://stackoverflow.com/questions/11599069/how-does-mongodb-sort-records-when-no-sort-order-is-specified) – Bonanza May 04 '16 at 12:30
  • Alright, so instead of disabling it, is there any other way for this issue? – Raghav Manikandan May 04 '16 at 18:23

1 Answers1

0

As mentioned in the comments, if you don't specify a sort order, there's no guarantee what order the results will come in. If you have a specific order in mind that is independent of any of the existing fields, then the best approach is to add an order field to the records that you can use to order them manually, and then sort on that.

sgress454
  • 24,870
  • 4
  • 74
  • 92