var friends = Users.find({_id: {$in: friendList}}, {sort: {onlineStatus: 1}});
This returns all user objects though. Might want to pluck some fields if you just want the IDs
The 1 after onlineStatus means ascending. If you want descending, use -1
First, have a look at what you’re doing on unsortedList. There are two problems. I assume you’re trying to extract only the profile.friends property for all matching objects?
Instead try doing:
var unsortedList = Meteor.users.find({_id: myid}, {fields: {'profile.friends':1}});
You have to specify “fields” and then list the fields you want. Just like you do with sort. The reason is namespace crashes. Imagine you have a field called field or sort.
You can, however, do this a lot simpler. Since this is the logged in user, just do:
var unsortedList = Meteor.user().profile.friends;
I’m now assuming that this list is on the form [“id1”, “id2”,…]. It needs to be on this form in order to be processed by $in.
From a security perspective (And in case they chance something), I’d stay away from putting full user objects in the friends-list. However, you can easily get a list of a single field from a list of documents using http://underscorejs.org/
the _pluck function returns an array like I suggested. From their docs:
Also. learn to use the JavaScript debugger in your browser or IDE or “console.log()”. Helps you figure stuff like this out. Because right now I think you’re in the “Something’s wrong, help me figure out what it is”-state instead of “this is wrong, how do I make it right?”-state