Created
January 5, 2016 11:56
-
-
Save lukejagodzinski/f59ac16adfaddd746dd4 to your computer and use it in GitHub Desktop.
Article: Sorting with Relations in Meteor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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