Reputation: 8686
I have a node with a list of user scores in a Firebase DB, I would like to order the list from highest to lowest and show only the top 10 scores:
My leaderboard node:
leader_board
-$week_id
-$user_id: score
And my java code is as follows:
Query leader_board_query = leader_board_reference.child(week_id).orderByValue().limitToLast(10);
For some odd reason, it always orders from lowest to highest. I have tried using limitToFirst(10)
and nothing changes, please help
Upvotes: 3
Views: 4166
Reputation: 8686
I managed to solve this painful problem. All I had to do was alter my recyclerview layout a bit by adding the following lines of codes to its layout manager.
LinearLayoutManager layout_manager = new LinearLayoutManager(getContext());
layout_manager.setReverseLayout(true);
layout_manager.setStackFromEnd(true);
The list got ordered in reverse which is resulted in ordering from highest to lowest.
Upvotes: 2
Reputation: 4029
orderByValue()
will always order by ascending order.
You can use limitToLast()
to get only the last section of the sorted items then you have to revert the items programmatically.
Example dataset:
1, 3, 8, 9, 0, 5, 4, 7, 6, 2
orderByValue().limitToFirst(3)
should return 0, 1, 2
orderByValue().limitToLast(3)
should return 7, 8, 9
, and then you revert the array to get 9, 8, 7
.
Note: This is all untested, please report back with results ;)
Upvotes: 5