title: CouchDB many-to-many fetching with dynamic sorting
body: <p>I have a CouchDB index where documents are music files with fields <code>artist</code>, <code>album</code>, <code>title</code>, etc. I also want to store playlists of tracks and be able to fetch all of a playlist's tracks and optionally sort the results by arbitrary fields.</p>
  <p>I've read through the <a href="https://docs.couchdb.org/en/stable/ddocs/views/joins.html" rel="nofollow noreferrer">documentation on joins</a> and the blog post/comment example is basically the same architecture as my playlist/track one, so I already have the ability to fetch a playlist's tracks and sort them by some predetermined key.</p>
  <p>Is there any way to combine a view (like in those docs) with post-hoc sorting by some other arbitrary field (<code>artist</code>, <code>title</code>, etc.)?</p>
  <p>I don't mind using multiple queries, but collecting all of the returned document IDs and using them in a subsequent <code>/_find</code> query like <code>{&quot;_id&quot;: {&quot;$in&quot;: [...]}}</code> query is extremely slow. Sorting them client side is always an option, but then I'd need to implement sorting logic twice in my app: a server-side version for when I'm using <code>/_find</code> on all tracks and a client-side version for tracks returned from a playlist view.</p>
  <p>(This would be trivially easy to accomplish with an RDBMS but my use case involves syncing data between mobile and desktop versions of the same app (which might be offline), which is what led me to CouchDB+PouchDB.)</p>