title: CouchDB sorting and filtering in the same view
body: <p>I'm trying to use CouchDB for a new app, and I need to create a view that sorts by multiple fields and also filters by multiple fields. Here is an example document, I've left out the _id and _rev to save myself some typing.</p>
  
  <pre><code>{
      "title": "My Document",
      "date": 1279816057,
      "ranking": 5,
      "category": "fun",
      "tags": [
          "couchdb",
          "technology"
      ],
  }
  </code></pre>
  
  <p>From the documentation, I've learned that I can easily create a view that sorts by a field such as ranking.</p>
  
  <pre><code>function(doc) {
      emit(doc.ranking, doc);
  }
  </code></pre>
  
  <p>I've also learned that I can easily filter by fields such as category</p>
  
  <pre><code>function(doc) {
      emit(doc.category, doc);
  }
  
  http://127.0.0.1:5984/database/_design/filter/_view/filter?key=%22fun%22
  </code></pre>
  
  <p>My problem is that I need to do a bunch of these things all at the same time. I want to filter based on category and also tag. I should be able to filter down to only documents with category of "fun" and tag of "couchdb". I want to sort those filtered results by ranking in descending order, then by date in ascending order, then by title in alphabetical order. </p>
  
  <p>How can I create one view that does all of that sorting and filtering combined?</p>