I recently delved into MeteorJS, Mongodb, and Iron:router, and wrote a small code to sort a list of websites based on user interactions. Initially, everything worked smoothly, but I encountered an issue with sorting near the end. Specifically, the websites should be sorted by the number of upvotes they have received, as well as the date of creation. Below are the key sections of the code:
//Sorting websites based on userfilter or votes
websites:function(){
if (Session.get("userFilter")){
return Websites.find({ createdBy: Session.get("userFilter") }, { sort: { createdOn: -1, up: -1 }});
} else {
return Websites.find({},{ sort: { createdOn: -1, up: -1 }});
}
},
{{#each websites}}
<div class="col-xs-12 col-md-3">
<div class="thumbnail">
<a href="{{url}}" class="site_name">{{title}}</a>
<p class="site_desc">{{description}}</p>
<br>
<p class="upvote_button">Upvote: {{up}}</p>
<p class="downvote_button">Downvote: {{down}}</p>
<a href="#" class="btn btn-default js-upvote" id="upvote_button">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
</a>
<a href="#" class="btn btn-default js-downvote" id="downvote_button">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
</a>
<br>
<p class="added-by">Added By:
<a href="#" class="js-filter">{{getUser createdBy}}</a>
</p>
<p>Added On: {{createdOn}}</p>
<a href="/single_website/{{_id}}">Details</a>
</div>
</div>
{{/each}}