Firebase Data Desc Sorting in Android

前端 未结 15 2188
醉话见心
醉话见心 2020-11-22 07:51

I am storing data in Firebase storage.

Object Comment with attribute timestamp. When I push data from device to Firebase I\'m populating

相关标签:
15条回答
  • 2020-11-22 08:15

    I found the current firebase version supports descending order sorting:

    citiesRef.orderBy("name", "desc").limit(3)
    

    https://firebase.google.com/docs/firestore/query-data/order-limit-data

    Hope it helps

    0 讨论(0)
  • 2020-11-22 08:17

    Firebase can order the items in ascending order by a given property and then returns either the first N items (limitToFirst()) or the last N items (limitToLast()). There is no way to indicate that you want the items in descending order.

    There are two options to get the behavior you want:

    1. Use a Firebase query to get the correct data, then re-order it client-side

    2. Add a field that has a descending value to the data

    For the latter approach, it is common to have a inverted timestamp.

    -1 * new Date().getTime();
    
    0 讨论(0)
  • 2020-11-22 08:22

    Swift 3:

        let query = firebase.child(YourQueryPath).queryOrdered(byChild: YourQueryChild).queryLimited(toLast: YourLimit)
    
        query.observeSingleEvent(of: .value, with: { (snapshot) in
             // Reverse order here to get top **YourLimit** results in descending order
        })
    
    0 讨论(0)
  • 2020-11-22 08:26

    I don't see any option to reverse the data. But One Brute way is to get the data.

    List<ModelClass> mList=new ArrayList();
    public void onDataChange(DataSnapshot dataSnapshot) 
    {
         mList.clear();
         for(DataSnapshot children: dataSnapshot.getChildren()){
            ModelClass modelClass=children.getValue(ModelClass.class);
            mList.add(modelClass);
         }
         Collections.reverse(mList);
         Adapter.notifyDataSetChanged();
    }
    
    0 讨论(0)
  • 2020-11-22 08:28

    Based on Himanshus answer but Im using kotlin and instead of the timestamp in the question Im ordering by score which should be similar. I'm not sure if firebase has an ascending or descending function but this is what ended working for me in my current project. I hope it will help someone else in the near future.

    FirebaseFirestore.getInstance().collection("leaderboard")
                .orderBy("score")
                .addSnapshotListener { snapshot, exception ->
    
                    if (exception != null) {
                        Log.e("Exception:", "Could not retrieve scores ${exception.localizedMessage}")
                    }
    
                    if (snapshot != null) {
                        scores.clear()
    
                        for (document in snapshot.documents) {
                            val data = document.data
    
                            val name = data?.get("name") as? String
                            val score = data?.get("score") as? Number
    
                            val documentId = document.id
    
                            val newScore = Score(name, score, documentId)
                            scores.add(newScore)
                        }
    
                        scores.reverse()
                        scoresAdapter.notifyDataSetChanged()
                    }
                }
    

    This is what worked for me. Happy coding!

    0 讨论(0)
  • 2020-11-22 08:30

    The @Monet_z_Polski approach, based on

    @Override
    public T getItem(int position) {
      return super.getItem(getCount() - (position + 1));
    }
    

    does have a weird effect on not update FirebaseRecyclerView automatically (PopulateViewHolder is not triggered in realtime changes). So, the best option is use a negative key to index the data.

    0 讨论(0)
提交回复
热议问题