Skip to content

Instantly share code, notes, and snippets.

@lukejagodzinski
Created January 5, 2016 11:56
Show Gist options
  • Save lukejagodzinski/f59ac16adfaddd746dd4 to your computer and use it in GitHub Desktop.
Save lukejagodzinski/f59ac16adfaddd746dd4 to your computer and use it in GitHub Desktop.
Article: Sorting with Relations in Meteor
Template.Users.helpers({
// Fetch posts and corresponding authors
var posts = Posts.find().fetch();
posts.forEach(function(post) {
post.author = Users.findOne(post.userId);
});
// Sort posts by a give field
posts.sort(function(a, b) {
if (a.get('author.name') < b.get('author.name')) {
return -1;
}
if (a.get('author.name') > b.get('author.name')) {
return 1;
}
return 0;
});
// Reverse posts array if in descending order
if ( /* If in descending order */ ) {
posts.reverse();
}
return posts;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment