Sorting a Meteor Cursor

I am using Ionic 2 with Meteor/Mongo.

I am trying to sort a Cursor, but find it just keeps the original order which the items were inserted.


  interface Chat {
    _id?: string;
    memberIds?: string[];
    title?: string;
    subTitle?: string;
    picture?: string;
    lastMessage?: Message;
    lastMessageCreatedAt?: Date;
    receiverComp?: Tracker.Computation;
    lastMessageComp?: Tracker.Computation;


  private sortLocalChats(): void {
    this.localChatCursor = this.localChatCollection.find({}, { sort: { lastMessageCreatedAt: -1 } });
    this.localChatCursor.forEach((chat: Chat) => {
      console.log(chat.title+'   '+chat.lastMessageCreatedAt);
    console.log('==> loaded sorted local chats: ' + this.localChatCollection.find().count());


Ashton Marais   Thu Oct 06 2016 16:50:36 GMT+0800 (CST)
Ashton Marais   Wed Oct 12 2016 21:20:18 GMT+0800 (CST)
ghjghj ghjghg   Wed Oct 05 2016 23:37:49 GMT+0800 (CST)
Brett Simpson   Thu Oct 06 2016 23:52:05 GMT+0800 (CST)
==> loaded sorted local chats: 4 

I would have expected this to be sorted by lastMessageCreatedAt.

Any help appreciated.

Iā€™m not 100% sure, but to get sorted results you probably need to call fetch() first. So in your example try if this works:

1 Like

That works, thank you