2

I am new in firebase, I want to sort data, by timestamp and my database is below, here key is timestamp

My database

My code for retrieving data is below

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("MainDoor");
Query dataOrderedByKey = myRef.orderByChild("{pushId}/key");
dataOrderedByKey.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

Map<String, MyListData> value = ((HashMap<String, MyListData>) dataSnapshot.getValue());

Here i am getting value is not by order of key

I am getting data like below which is not in sort order

Data which I am getting

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
Rocky Jems
  • 23
  • 4

4 Answers4

4

Data in a Map is by definition not sorted. So when you call dataSnapshot.getValue(), all information about the order of the items in the DataSnapshot is list.

To maintain the order, use a data structure that supports that, such as a list, and extract the individual items in the correct order by looping over DataSnapshot.getChildren().

So something like:

dataOrderedByKey.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
      System.out.println(snapshot.getKey());
      MyListData value = snapshot.getValue()
    }
  }
  ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you so much @Frank van Puffelen, it's working – Rocky Jems Sep 19 '19 at 12:25
  • ohh.. thanks for guidance, I have tried to first up-vote your answer but it shows that I need more than 15 reputation, but anyway thank you so much for your support, you have saved my lots of money, because time is money and you have saved my time.... – Rocky Jems Sep 19 '19 at 14:55
0

Also you can get data from firebase in sorted order . Refer my answer at Sort Firebase data in ascending/Descending Order

Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14
0

You want to sort by date, right? Try it

   FirebaseDatabase database = FirebaseDatabase.getInstance();
   DatabaseReference myRef = database.getReference("MainDoor");
   myRef.orderByChild('date').addChildEventListener(new ChildEventListener() {
    // implement the ChildEventListener methods as documented above

   });
0

Because of the way JavaScript objects work, the ordering of data in the JavaScript object returned by val() is not guaranteed to match the ordering on the server nor the ordering of child_added events. That is where forEach() comes in handy. It guarantees the children of a DataSnapshot will be iterated in their query order.

    // Assume we have the following data in the Database:
{
  "users": {
    "ada": {
      "first": "Ada",
      "last": "Lovelace"
    },
    "alan": {
      "first": "Alan",
      "last": "Turing"
    }
  }
}

// Loop through users in order with the forEach() method. The callback
// provided to forEach() will be called synchronously with a DataSnapshot
// for each child:
var query = firebase.database().ref("users").orderByKey();
query.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      // key will be "ada" the first time and "alan" the second time
      var key = childSnapshot.key;
      // childData will be the actual contents of the child
      var childData = childSnapshot.val();
  });
});
5huraif
  • 1
  • 3