12

Using firebaseArray, is it possible to order by a specified child key: (https://www.firebase.com/docs/web/guide/retrieving-data.html#section-queries)?

var fireRef = new Firebase(https://dinosaur-facts.firebaseio.com/dinosaurs);
var query = fireRef.orderByChild("echo").limitToFirst(25);
var todos = $firebaseArray(query);

However, by default, Firebase retrieves the keys in ascending order.

Children with a numeric value come next, sorted in ascending order.

Is there any way to get results in the descending order directly from firebaseArray? For example, do they have anything like .reverse() or .orderBy(desc, ...)?

divibisan
  • 11,659
  • 11
  • 40
  • 58
Sung Kim
  • 8,417
  • 9
  • 34
  • 42
  • 1
    possible duplicate of [Display posts in descending posted order](http://stackoverflow.com/questions/25611356/display-posts-in-descending-posted-order) – Frank van Puffelen Jul 08 '15 at 03:19
  • I saw that, but that's a complicated solution. Do they have anything .reverse() or .orderBy(desc, ...)? – Sung Kim Jul 08 '15 at 04:33
  • 4
    *Disclaimer: I work for Firebase* Nope. We're aware that such an addition would be useful, but it doesn't currently exist. In some cases you can invert your values to get the same result (i.e. `-1 * timestamp`). But that depends on the type of value you're storing. – Frank van Puffelen Jul 08 '15 at 13:26
  • @FrankvanPuffelen Wonderful idea. This solved my problem. Thanks! – Sung Kim Jul 08 '15 at 14:51
  • 2
    Is there an example of the -1 * timestamp? This would be a very useful feature for Firebase – Jordash Nov 29 '15 at 19:03
  • How i can multiply -1 * server value? [FirebaseServerValue.timestamp()] – Andrey Gagan Apr 08 '16 at 12:09
  • @AndreyGagan I just inserted '1*timestap' when I insert other data. – Sung Kim Apr 10 '16 at 04:34
  • @sung-kim What? So you write two times? It will perform mutable state. You cannot do this, it's bad. – Andrey Gagan Apr 12 '16 at 08:18
  • @AndreyGagan I am not sure I understand this. Adding one more field won't hurt too much in my case. I meant -1*timestamp. – Sung Kim Apr 12 '16 at 08:37
  • @FrankvanPuffelen will queryOrderedByChild("negative_timestamp") be slow when you have 100K of items? give that they are reversely stored and need to be reversely sorted each time? – Muhammad Hassan Nasr Nov 13 '16 at 17:57

2 Answers2

14

This doesn't directly answer the original question, but for anyone using FirebaseUI Java/Android, with a RecyclerView, it's possible to order the RecyclerView in reverse, rather than using a query on Firebase:

Example:

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

(Ensuring you set both before adding the layout manager to the RecyclerView also)

Github issue: https://github.com/firebase/FirebaseUI-Android/issues/90#issuecomment-230643303

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Alan
  • 1,510
  • 1
  • 18
  • 35
  • 1
    This is exactly what I wanted! Thanks a bunch! – Alexey Sep 08 '16 at 03:25
  • 1
    Thanks a lot pal, that's just what I need – rkmax Nov 09 '16 at 01:45
  • 1
    Much appreciated Alan, exactly what I was looking for. Is this more efficient code compared to trying to actually re-order the firebase database? – tccpg288 Dec 03 '16 at 20:59
  • I haven't seen any documentation suggesting reversing the layout manager affects performance on android. Performance didn't diminish in my case anyway. It saved me the headache of dealing with more firebase queries and timestamps also – Alan Dec 03 '16 at 21:17
1

Here is how you can order by date. Just use queryOrderedByChild(dateKey) and it will be in asc order, to make desc you can do array.reverse()

POST_REF.queryOrderedByChild(PostKeys.kPostDate).observeEventType(.Value, withBlock: {
  snapshot in

  var localPosts = [Post]()
    for item in snapshot.children {
      let postItem = Post(snapshot: item as! FDataSnapshot)
      localPosts.append(postItem)
    }
  //Display at desc order
   let reverseResults = localPosts.reverse() as [Post]
       completion(postsArray: reverseResults)
    }, withCancelBlock: { error in
   completion(postsArray: nil)
})
the_pantless_coder
  • 2,297
  • 1
  • 17
  • 30
Svitlana
  • 2,938
  • 1
  • 29
  • 38