William
William

Reputation: 4588

Meteor.js: Understanding sort parameters within a find{} method

I am doing exercises in the book "Getting Started With Meteor.js Javascript Framework" and there is a section where a template called categories is created, then a {#each} loop thumbs through a collection.This is done with the intent of printing the results to the screen. This works and the code is below.

<template name ="categories">

    <div class ="title">my stuff</div>

    <div id='categories'>
        {{#each lists}}

        <div id="category">

            {{Category}}

        </div>

        {{/each}}
    </div>

</template>

My question is this:

In the code below the sort parameter doesn't seem to do anything. I am curious what it is supposed to be doing. I understand that theoretically it's suppose to "sort", but when I play with the parameters nothing changes. I looked it up in the meteor documentation and I couldn't figure it out. I am trying to modify the code so that it so that it sorts in a different order. This way I can see results and get a real understanding of how {sort: works. I tried modifying the {sort: {Category: 1}}) to {sort: {Category: -1}}) as well as {sort: {Category: 2}}) and it's always the same result.

  Template.categories.lists = function() {
    return lists.find({}, {sort: {Category: 1}});  // Works

  /* return lists.find({}); */                     // Works just the same

  }

The Collections are as follows:

lists.insert({Category:"DVDs", items: [{Name:"Mission Impossible" ,Owner:"me",LentTo:"Alice"}]});

lists.insert({Category:"Tools", items: [{Name:"Linear Compression Wrench",Owner:"me",LentTo: "STEVE"}]});

Upvotes: 3

Views: 11832

Answers (2)

L.T
L.T

Reputation: 2589

In your code, you can attempt to change the argument of the sort to be an array instead of an object, like this:
sort: [["Category","asc"],["another argument","desc"],[...]]
so the code :

 Template.categories.lists = function() {
    return lists.find({}, {
        sort: [
            ["Category", "asc"],
            ["another argument", "desc"],
            [...]
        ]
    }); 
    // return lists.find({}); // Works just the same
}

Upvotes: 3

Travis G
Travis G

Reputation: 1602

As per my understanding that is because default is to sort by natural order. More info can be found at this link

Upvotes: 0

Related Questions