title: Mongoose, sort query by populated field
body: <p>As far as I know, it's possible to sort populated docs with Mongoose (<a href="https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/6t-AzCSPgHs" rel="noreferrer">source</a>).</p>
  
  <p>I'm searching for a way to sort a query by one or more populated fields.</p>
  
  <p>Consider this two Mongoose schemas :</p>
  
  <pre><code>var Wizard = new Schema({
      name  : { type: String }
  , spells  : { [{ type: Schema.ObjectId, ref: 'Spell' }] }
  });
  
  var Spell = new Schema({
      name    : { type: String }
  ,   damages : { type: Number }
  });
  </code></pre>
  
  <p>Sample JSON: </p>
  
  <pre><code>[{
      name: 'Gandalf',
      spells: [{
              name: 'Fireball',
              damages: 20
          }]
  }, {
      name: 'Saruman',
      spells: [{
              name: 'Frozenball',
              damages: 10
          }]
  }, {
      name: 'Radagast',
      spells: [{
              name: 'Lightball',
              damages: 15
          }]
  }]
  </code></pre>
  
  <p>I would like to sort those wizards by their spell damages, using something like :</p>
  
  <pre><code>WizardModel
    .find({})
    .populate('spells', myfields, myconditions, { sort: [['damages', 'asc']] })
  // Should return in the right order: Saruman, Radagast, Gandalf
  </code></pre>
  
  <p>I'm actually doing those sorts by hands after querying and would like to optimize that.</p>