Jay Patel
Jay Patel

Reputation: 2451

Firebase sorting data based on child not working

I'm trying to develop a simple application to learn firebase database.

Here is my realtime database schema

mydirectory
 -contact_list
   -LOyDhepB_IZlM6lZtko
      mobileNumber: "9385746982"
      name: "Jay"
   -LOyDhetiPHalLhXrOaU
      mobileNumber: "8000478912"
      name: "Ravi"
   -LOyDhetiPHalLhXrOaV
      mobileNumber: "123456789"
      name: "ABC"
   -LOyDheubruATyyBp8dG
      mobileNumber: "023456879"
      name: "XYZ"

I want to get the list of data ordered by name

So I'm using this snippet to load the data in my android application

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("contact_list").orderByChild("name").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, Object> objectMap = (HashMap<String, Object>) dataSnapshot.getValue();
            Master.personList.clear();
            for (Object obj : objectMap.values()) {
                if (obj instanceof Map) {
                    Map<String, Object> mapObj = (Map<String, Object>) obj;
                    String name = (String) mapObj.get("name");
                    String mobileNumber = (String) mapObj.get("mobileNumber");
                    Person person = new Person(name, mobileNumber);
                    Master.personList.add(person);

                    populateListView();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("ANDs", "Failed to read value.", error.toException());
        }
    });

But it does not sort data based on the alphabetical order of name key. Am I doing anything wrong or missing something?

P.S.

I've also indexing rules (ref), but still result is same! It doesn't sort data alphabetical wise.

{
 "rules": {
    "mydirectory": {
        "contact_list": {
        "$user_id": {
          ".indexOn": ["name"]
        }
        }
    },
    ".read": true,
    ".write": true
  }
}

I've tried many things from other posts like this topic but nothing works! Please don't make it duplicate instead please help me to solve it!

Upvotes: 1

Views: 636

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598785

A HashMap can only contain a key and a value. And the keys in a Map are by definition not ordered. So when you call dataSnapshot.getValue(), you're throwing away all ordering information.

To maintain the order, you'll need to loop over dataSnapshot.getChildren():

public void onDataChange(DataSnapshot dataSnapshot) {
    Master.personList.clear();

    for (DataSnapshot contactSnapshot: dataSnapshot.getChildren()) {
        String name = contactSnapshot.child("name").getValue(String.class);
        String mobileNumber = contactSnapshot.child("mobileNumber").getValue(String.class);
        Person person = new Person(name, mobileNumber);
        Master.personList.add(person);
    }

    populateListView();
}

You'll also notice that I use child(...) to get a child snapshot for a specific property and get the String value from it. Your approach for that would work too, but I find that I get better error messages when I stick to a snapshot longer (instead of converting to a Map).

Upvotes: 5

Related Questions