LokiJs collection find return list in Ascending order

Viewed 3857

How do i get to sort a loki js collection in a ascending order

var categoryCollection = offlineDb.getCollection('Category');
var catColl = categoryCollection.find({ 'Id': { '$ne': null } });

I debug and i see that the $loki is also in the reverse order . This returns the list in a reverse order ! How can i perform a find with the list in ascending order ? Isnt it by default when you do a find return in a ascending order ?

2 Answers

Note that simplesort() requires chain(), which returns a ResultSet instead of a standard Array:

categoryCollection.chain().find({ 'Id': { '$ne': null } }).simplesort('Id');

If you just need the Array with a flipped order, use Array.reverse():

categoryCollection.find({ 'Id': { '$ne': null } }).reverse();

If reverse() still isn't enough to sort the data, use a chain(), simplesort(), data() sandwich to return a standard Array:

categoryCollection.chain().find({ 'Id': { '$ne': null } }).simplesort('Id').data();
Related