0

I want to make leaderboard by country. I tried such query:

.orderByChild(COUNTRY_COLUMN)
.equalTo(country)
.orderByChild(TROPHIES_COLUMN)

But Firebase don't allow combine multiple orderBy calls. How can I make such query?

markrof
  • 3
  • 1
  • 1
    Possible duplicate: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Alex Mamo May 11 '17 at 14:04

1 Answers1

0

There are multiple methods to achieve this, one is to filter by country and then on the client order by trhopies, you can also create another column in firebase that would be the index, something like:

country_trophies_index: 'US_12'

and then just do the orderby that property.

You can see more details in this answer:

https://stackoverflow.com/a/26701282/641345

Community
  • 1
  • 1
Escobar5
  • 3,941
  • 8
  • 39
  • 62
  • I tried extra column method: `.orderByChild("country_trophies_index") .startAt("US_")` But it has incorrect sorting: `1. country_trophies_index: "US_2000" 2. country_trophies_index: "US_540" 3. country_trophies_index: "US_550"` – markrof May 12 '17 at 13:11
  • That's because it's sorting as text, so "540" is after "2000", you can try to add leading zeroes an always use same length in numbers, like "US_0000002000" and "US_0000000540" – Escobar5 May 12 '17 at 15:36
  • it works in such way, but the problem is that Firebase sorts in ascending order – markrof May 12 '17 at 19:11
  • I've used another trick for that one, just take a very large number and substract the trophies number, like this: "US_9999997999" which is the same as "US_" + (9999999999-2000) – Escobar5 May 12 '17 at 20:07