Firebase Query Ordering Not Working Properly

Firebase query ordering by child not working

Firebase Realtime Database results are always in ascending order. There is no way to get them in descending order from the database, so you will have to reverse the results in your application code.

A simple option for this is to insert each item at the top of the table, instead of appending it to the bottom as you do now:

self.table.insert(video, at: 0)

The alternative would be to store inverted values in your database, like: userTime: -1654342137807. That way the ascending order that the database uses, will actually be what you want.

Also see:

  • How to get data from firebase in descending order of value?
  • Swift - How to create Sort query as Descending on Firebase?
  • Firebase query sort order in swift?

Firebase Sorting is not working properly in flutter

It looks like the values in your episodeNum field are stored as string values, and not as numbers. And in the lexicographical ordering (which Firestore uses for string values) "10" comes before "2", just as "aa" comes before "b".

To fix the problem, store the episodeNum values as numbers in the database, and Firestore will order them correctly.

Firebase orderByChild Order Is Not Correct

Your scores are strings. They should be numbers instead. Strings don't sort like numbers.

Firestore query orderBy not working?

I think you are missing a ' here '_timeStampUTC, so it should be:

 orderBy('_timeStampUTC', descending: true)

EDIT:

Also, you need to be sure to create an index for toid and other for _timeStampUTC, this is done when you try to order by a property that is not in you the where of the query.



Related Topics



Leave a reply



Submit