js-data-rethinkdb icon indicating copy to clipboard operation
js-data-rethinkdb copied to clipboard

orderBy multiple fields

Open bymaximus opened this issue 5 years ago • 4 comments

Is there some way to use orderBy with more than one field? For example, this orderBy(r.desc('fieldA'), r.desc('fieldB')) have different results from orderBy(r.desc('fieldA')).orderBy(r.desc('fieldB'))

bymaximus avatar Dec 01 '19 02:12 bymaximus

orderBy([“user”, “updatedAt”])

Sent from my iPhone

On Nov 30, 2019, at 6:45 PM, bymaximus [email protected] wrote:

 Is there some way to use orderBy with more than one field? For example, this orderBy(r.desc('fieldA'), r.desc('fieldB')) have different results from orderBy(r.desc('fieldA')).orderBy(r.desc('fieldB'))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

crobinson42 avatar Dec 01 '19 02:12 crobinson42

orderBy([“user”, “updatedAt”])

This will order by ASC, seems the code is hardcoded to get the sort direction from the second element of the array, which makes impossible to use for example orderBy(r.desc('fieldA'), r.desc('fieldB'))

    // Sort
    if (query.orderBy) {
      if (jsData.utils.isString(query.orderBy)) {
        query.orderBy = [[query.orderBy, 'asc']];
      }
      for (var i = 0; i < query.orderBy.length; i++) {
        if (jsData.utils.isString(query.orderBy[i])) {
          query.orderBy[i] = [query.orderBy[i], 'asc'];
        }
        rql = (query.orderBy[i][1] || '').toUpperCase() === 'DESC' ? rql.orderBy(r.desc(query.orderBy[i][0])) : rql.orderBy(query.orderBy[i][0]);
      }
    }

bymaximus avatar Dec 01 '19 17:12 bymaximus

Here's your 2 options:

orderBy(['user', 'DESC'])
// or
orderBy([ ['user', 'DESC'], ['updatedAt', 'asc']])

crobinson42 avatar Dec 01 '19 18:12 crobinson42

This lib looks like it needs some attention. @bymaximus if you'd like to help contribute to the code base here, I'm more than happy to help you push PR's along 👍🏽

crobinson42 avatar Dec 01 '19 18:12 crobinson42