How to sort a Cursor, or how to sort twice?


#1

I want:
myCollection.find({}, {sort: [['createdAt','asc']], limit: queueLength - 1}).sort([['value','desc']])

I thought I should be able to because find().sort() appears to exist in the MongoDB documentation (ref). But Collection.find() inside Meteor does not return a Cursor object that can call the sort function.

What is a good solution?

Currently I’m looking at using UnderscoreJS: _.sortBy(myCollection.find().fetch(), 'value').reverse(); but was hoping for something better.


#2

that underscore+array prototype function combo seems good.
if you need more sorts, this one seems promising too https://github.com/Teun/thenBy.js


#3

You could do myCollection.find({}, {sort: { createdAt: 1, value: -1 }, limit: queueLength - 1});


#4

@hellstad - wouldn’t that just use the value field as a tiebreaker when createdAt was equal?