-1

I'm trying to retrieve all field names from a document (the document name is the userID) under a collection called "taskboards". I want Firestore to retrieve these field names in a specific order but I don't know how.

Here is the following code:

userID = fAuth.getCurrentUser().getUid();
DocumentReference userTaskboardRef = fStore.collection("taskboards").document(userID)

userTaskboardRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if(task.isSuccessful()){

            List<String> columnsList = new ArrayList<>();

            Map<String, Object> map = task.getResult().getData();
            for(Map.Entry<String, Object> entry : map.entrySet()){
                columnsList.add(entry.getKey());
                Log.d("TAG", entry.getKey());
                Log.d("TAG", entry.getValue().toString());

            }

For example:

I would like Firestore to retrieve the following field names in the order "To Do, Doing, Done". Here is a picture of how the field names are stored in my Firestore.

enter image description here

The above code works and retrieves the field names but it retrieves them in the order "Doing, To Do, Done". Here is a picture of TAG showing the order.

enter image description here

I'm quite new to Android Studio so can anybody help me out? Is it even possible to specify the order in which the field names are retrieved?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

The order of fields in a document is undefined, as is the order of entries in a Map.

The screenshot of the Firebase console in your questions shows the fields in alphabetical order. If you want to replicate that in your code, you can sort the keys with a TreeMap:

Map<String, Object> map = new TreeMap<>(task.getResult().getData());
for(Map.Entry<String, Object> entry : map.entrySet()){
    ...

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807