Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a client-side-only (local) Meteor Collection

Tags:

meteor

I have client side only (local) Meteor collection defined like that (coffeescript):

Products = new Meteor.Collection null

However when I try to find() providing sorting parameters Meteor tells me that sorting of local collections is not supported. This is understandable.

I would like to know what is the easiest/simplest way to get sorted results. Essentially I always use all the data in the Collection, so keeping it in sorted state would solve the problem.

like image 430
babbata Avatar asked Dec 07 '13 15:12

babbata


1 Answers

It works for me, are you using the latest version of Meteor? Running this code works on the Meteor Docs site:

var foos = new Meteor.Collection( null );
for ( var i = 0; i < 100; i++ ) {
  foos.insert({ num: i });
}
foos.findOne( {} ).num; // => 0
foos.findOne( {}, { sort: [[ "num", "desc" ]] } ).num; // => 99
like image 132
sbking Avatar answered Nov 15 '22 10:11

sbking