1

I have news data in Firebase in following format:

enter image description here

I want to sort it according to date (newsDate) in descending order (latest on the top). I have added this to my code while I am retrieving data from Firebase:

databaseReference.orderByChild("newsDate").addChildEventListener(new ChildEventListener() { // on child added methods }

But this only does string sorting, so that order becomes like this: 04/08/2018 12:00, 13/08/2018 10:30, 30/07/2018 00:00

I know this is because of alphabetical sorting. Wondering how I can customize sorting order with my date format (dd/mm/yyyy hh:mm) and also in descending order?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    You need to use the Timestamp, firebase cannot sort a date string. https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp – Yayo Arellano Oct 12 '18 at 08:06
  • I need to sort without changing the date type in Firebase. So something in the front part should be done. Like collecting all data into an array and then sort it, then publish? Or publish it on the go to the correct order, so that ordering still correct? – Hasan Hasanzade Oct 14 '18 at 09:57
  • I had the same problem and solved it on this question: https://stackoverflow.com/questions/64677530/how-to-receive-data-ordered-by-date-on-firebase-real-time-database/64677531#64677531 – Aziz Nov 04 '20 at 10:36

2 Answers2

1

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi Hasan! Have you tried my solution above, does it work? – Alex Mamo Oct 13 '18 at 03:59
  • Sorry, but this didn't help me. Because I cannot change the property of newsDate in Firebase. I just need to sort what exists on Firebase. Now thinking another way of doing it. Like collecting all data into an array and then sort it, then publish? Or publish it on the go to the correct order, so that ordering still correct? – Hasan Hasanzade Oct 14 '18 at 09:59
  • No, is not! If the answer to an issue of your is that it **cannot** be solved the way you want it doesn't mean that the answer is not correct. It didn't helped you the way you expected but that's the way Firebase works. The simple and correct way of solving this issue is to change the type of property. Is can be done by simply converting the actual String to a `timestamp`. If you decide to order Strings, note that the elements will be ordered lexicographically and for sure this is not what you want. – Alex Mamo Oct 14 '18 at 10:14
0

instead of date try saving milli seconds and then simple use query like below.or simply query on date.

Query Using Millis

databaseReference.orderByChild("newsDate").startAt(new DateTime().getMillis())

Query Using Date

databaseReference.orderByChild("newsDate").startAt(new DateTime())

Or

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String currentDateandTime = sdf.format(new Date());

databaseReference.orderByChild("newsDate").startAt(currentDateandTime )
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47