Firebase Data Desc Sorting in Android

Firebase Data Desc Sorting in Android

Firebase can order the items in ascending order by a given property and then returns either the first N items (limitToFirst()) or the last N items (limitToLast()). There is no way to indicate that you want the items in descending order.

There are two options to get the behavior you want:

  1. Use a Firebase query to get the correct data, then re-order it client-side

  2. Add a field that has a descending value to the data

For the latter approach, it is common to have a inverted timestamp.

-1 * new Date().getTime();

Sorting events according to date in Firebase

Wondering how I can customize sorting order with my date format (dd/mm/yyyy hh:mm)?

You can solve this by changing the type of your newsDate property from String to timpestamp. Please take a look at my answer from this post to see how you can achieve this.

and also in descending order?

To solve this, please see Frank van Puffelen's answer from this post.

Sort Firebase data in ascending/Descending Order

You can get sorred data in ascending order from firebase using orderByChild function.you will need to create indexes on table .
Refer firebase sorting data section
https://firebase.google.com/docs/database/android/lists-of-data

// Descending order:

Do this task at client side as you already have sorted list. Use collections inbuilt function to reverse the list Collections.reverse(inputList)

Hope this helps you :)

How to descending order in Firebase realtime with pagination?

So, you've combined 3 questions into 1:

  • Why isn't my code sorting at all?
  • Why can't I sort in descending order?
  • Given a query that works, how should I do pagination with Firebase RTDB on Android?

Good news, the later 2 of these have good existing answers on StackOverflow!

First, the way you're using Query here:

Query query = mReference.limitToLast(page * TOTAL_ITEMS_TO_LOAD);
query.orderByChild("rating");

isn't correct (and this is why you aren't getting sorted results at all).

Instead try this:

Query query = mReference
.limitToLast(page * TOTAL_ITEMS_TO_LOAD)
.orderByChild("rating");

If you just call orderByChild, it returns a query that does that, but in your code you are ignoring the result.

To help make it clearer what is happening, you could also do this and it would work (but your current code doesn't save the result of the orderByChild call:

Query query = mReference.limitToLast(page * TOTAL_ITEMS_TO_LOAD);
query = query.orderByChild("rating");

Second, you can't sort by descending order in Firebase RTDB, so you should look at that question for possible solutions.

Finally, your current code will always be returning larger and larger numbers of results as page increases -- you don't always get just TOTAL_ITEMS_TO_LOAD results. A more complete SO question about pagination is here.

Sorting in descending order in Firebase database

Unfortunately firebase doesn't allow returning by descending order. I have generally just reversed the order of the results returned client side.

If your data query is too large to sort client side you can do

firebase.database().ref('books').orderByChild('ups').limitToLast(2)

and then invert the results from there. Documentation can be seen here

OrderByChild() is not sorting my firebase data

Data in a Map is by definition not sorted. So when you call dataSnapshot.getValue(), all information about the order of the items in the DataSnapshot is list.

To maintain the order, use a data structure that supports that, such as a list, and extract the individual items in the correct order by looping over DataSnapshot.getChildren().

So something like:

dataOrderedByKey.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
System.out.println(snapshot.getKey());
MyListData value = snapshot.getValue()
}
}
...

How to arrange firebase database data in ascending or descending order?

If you want to order the results of a Firebase database query, you need to use numbers as values and not Strings. If you keep the Strings, as I see in your screenshot, you need to know that the Strings are ordered lexicographically and I'm sure this is not what you are looking for.

The simplest way to order data in ascending or descending order, would be on client side. Firebase can order the results in ascending order by a given property by default. There is no method in Firebase to order in descending order but there a few tweaks that can be made.

If you are using a RecyclerView to display data, the simplest way would be to use the following code:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

This approch will reverse you the order.

Another approach would be to create your own adapter that extends FirebaseListAdapter and override getItem() method.

Another approach would be to get the data from the database, add it to a Collection, a List would be a good solution and then use Collections.reverse(yourList);.

Sort data to RecyclerView based on latest date from Firebase

I am developing an android chat application in which I need to order the conversation details by the date.

As I see in your screenshot, your Date property is of type String. This means that you cannot call:

.orderByChild("Date")

And expect to behave as it was a Timestamp. When you order String elements, the order that you get is always lexicographically. This means that Strings doesn't consider any sort of numeric values when sorting, especially when it comes to the dates, even if the dates contain numbers, as your first element does:

Date: "30/7/2021"

So using String values when querying your database it's not an option. However, I see you already have a Timestamp property. Maybe on that property, it was supposed to do the ordering. If that was not the case, I suggest you change the type of the Date property from String to Timestamp, as explained in my answer from the following post:

  • How to save the current date/time when I add new value to Firebase Realtime Database

Now I want to retrieve and show the data on the latest date on my RecyclerView

This means that most likely you need to reverse the order, meaning that all your transactions have to be displayed in your RecyclerView descending. In this case, there are a few options that you have, either on the server or on the client.

Assuming that you have changed the type of your Date property from String to Timestamp, then you can simply consider storing an inverted Timestamp value like this:

Firebase-root
|
--- transactions
|
--- 1
|
--- Date: 1627714194
|
--- invertedDate: -1627714194

See, the invertedDate property holds a negative value. Since by default, the elements are ordered ascending, to be able to order the transaction desecendiong, you should simply use:

Query query = nm.orderByChild("invertedDate").limitToFirst(5);

On the other hand, there are some workarounds that can be made to achieve the same thing on the client, as explained in my answer from the following post:

  • How to arrange firebase database data in ascending or descending order?


Related Topics



Leave a reply



Submit