question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for multiple `OrderBy` queries.

See original GitHub issue

I would like to see the ability to chain multiple OrderBy queries to filter $firebaseArray.
An example is fleshed out here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase

Is this anywhere on the horizon for Firebase? My application sorely needs it.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
jamestalmagecommented, Sep 18, 2015

As discussed in the link you referenced:

multiple orderBy() calls will throw an error, because the clients currently give unexpected results

This is a Firebase core issue, and has little to do with AngularFire.

Depending on your dataset, and what you are trying to do, this can be accomplished.

  1. If there is NOT a lot of data - pull everything and just sort on the client

    See angular’s orderBy filter.

  2. If there IS a lot of data - create synthetic indexes yourself

    On the client side, when you would normally do:

    ref.set({
      firstName: 'John',
      lastName: 'Doe',
      age: 32
    });
    
    ref.set({
      firstName: 'John',
      lastName: 'Doe',
      age: 32,
      firstLastAge: 'john~doe~032',
      lastFirstAge: 'doe~john~032',
      lastAgeFirst: 'doe~032~john',
      // and whatever other ordering you want to index on
    });
    

    Note that I have done a couple things with the generated index values:

    A. I’ve used ~ character as a separator, it is basically the last ASCII character. The goal here is that john~doe will still come after johny~appleseed. If you are allowing users to store characters that are a superset of ASCII you will need to pick a different separator that is lexographically after whatever the highest allowed character is.

    B. I’ve lowercased everything in the indexes. Firebase process orderBy queries lexicographically, this leads to some unexpected results for the uninitiated:

    Original Dataset: [ C, d, A, b ]
    Alphabetically Ordered: [ A, b, C, d ]
    Lexicographically Ordered: [A C b d]
    

    This has everything to do with the byte value of characters (an alphabetical sorting algorithm would be significantly more expensive).

    C. I’ve padded leading 0’s on the age. This allows users to be up to 999 years old. For bigger numbers you need more padding digits.

    As you can already tell, this quickly gets unmanageable if you need to be able to sort LOTS of different ways. You are also creating a bandwidth and storage penalty by creating those, so that needs to be weighed against the bandwidth penalty of option 1 above. This is similar to the exploding index problems you (used to?) run into on an AppEngine datastore. There are good talks on that topic that you should google if you are seriously considering this.

Other Thoughts

  1. You will want to write security rules to enforce correct indexes.
  2. Only store the indexes you know you need, you can always add future indexes using the REST api.
  3. As discussed, this quickly becomes unrealistic. If you want to give users a table with many thousands of entries, and a way to sort on any one of a dozen columns, Firebase may not be the tool you seek.
4reactions
karol-depkacommented, Sep 20, 2017

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL multiple column ordering - Stack Overflow
If you want to select records from a table but would like to see them sorted according to two columns, you can do...
Read more >
Multiple orderby on query - WordPress.org
Hello, I can't do a multiple sort with the post date field and a custom field. If I use only one field, the...
Read more >
How to Order By Two Columns in SQL? - LearnSQL.com
This query returns sorted records according to two columns: salary and last_name. ... After the ORDER BY keyword, add the name of the...
Read more >
SQL Server ORDER BY clause By Practical Examples
This tutorial shows you how to use the SQL Server ORDER BY clause to sort the result set of a query by a...
Read more >
Sort Multiple Columns in Queries - Visual Database Tools
To sort query results by more than one column · In the Criteria pane, click the Sort Order field for the column upon...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found