0

First of all, I am coding in Kotlin.

I'd like to arrange the posts stored in the Realtime Database for a certain period of time based on the number of likes and display them in the RecyclerView.

my database structure

"posts": {
  "key": {
    "uid": "NHYVkW...",
    "title" : "Title",
    "time": "1666852764028",
    "like" : 2
  }

I've read about methods orderByChild(), startAt(), and endAt(), but I don't know how to use them in combination.

The examples I've seen set startAt() and endAt() for the key that enters orderByChild(). But I want the sort to be based on like and the start and end points based on time.

Any examples I can refer to? Please help.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
opvsvjoo
  • 47
  • 2
  • 5
  • So basically you want to filter the data based on two fields, right? – Alex Mamo Oct 28 '22 at 12:50
  • I want to filter data by time and sort by number of likes. – opvsvjoo Oct 28 '22 at 12:52
  • 2
    You can't use multiple `orderBy()` in same query. You can fetch all the posts in the date range and then sort based on likes on the client side. Also checkout [this answer](https://stackoverflow.com/questions/37952747/firebase-multiple-where-clause-in-query?noredirect=1&lq=1) – Dharmaraj Oct 28 '22 at 13:31
  • 1
    For your use-case, the best approach is to use the database to filter on the `time` and then re-order the results on `like` value in your application code. – Frank van Puffelen Oct 28 '22 at 14:03

0 Answers0