0

I have a collection model that looks like this :

enter image description here

How do I sort the query from latest to oldest via the createdAt field.

So far I tried

db.ref('accounts')
    .startAt()
    .orderByChild('createdAt')
    .once('value')
    .then(function(snap){
        // do shits here...
    })

But am getting the data the oldest on top.

  1. January
  2. February
  3. March

But I want

  1. March
  2. February
  3. January
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jofftiquez
  • 7,548
  • 10
  • 67
  • 121
  • you can reverse your list after getting but it's better to get reversed from firebase http://stackoverflow.com/a/3963075/3496570 or http://stackoverflow.com/a/10766502/3496570 – Zar E Ahmer Oct 25 '16 at 06:20
  • Those are java solutions, well anyways thanks for the idea :) @Nepster – jofftiquez Oct 25 '16 at 07:50
  • try limitToLast like http://stackoverflow.com/a/33484784/3496570 or http://stackoverflow.com/a/38024909/3496570 – Zar E Ahmer Oct 25 '16 at 08:01

1 Answers1

1
  1. There is no method to get items sorted in descending order in firebase database. So you need to sort them by ascending and reverse the array in your programming language.

  2. If you want to sort them by time you should be using timestamp instead of datetime as string. In this case you will get wrong ordering because they are actually compared as strings. For example: Imagine this two dates 2016-10-14T02:30:59.653Z, 2016-10-2T02:30:59.653Z. As dates first one is greater but as a strings (lexicographically) second one is greater.

Zura Sekhniashvili
  • 1,147
  • 7
  • 19