It's more of a Java question (although I'm using Firebase). I'm trying to understand what's best way to sort a list. I'm trying to create the following map:
Map<Character,List<QueryDocumentSnapshot>> docs;
In order to build this map, I iterate over the documents in my collection, get the first letter of each one and insert the document into the list where the key is the letter. The code:
for (QueryDocumentSnapshot current_document : value) {
char letter = getFirstLetter(current_document.getString("type"));
List<QueryDocumentSnapshot> list = docs.get(letter);
if (list == null) {
list = new ArrayList<>();
list.add(current_document);
docs.put(letter, list);
} else {
list.add(current_document);
}
}
In order to make the docs
map sorted, I use TreeMap
:
docs = new TreeMap<>();
But how can I make the list of documents be sorted by the field type
(String)?
EDIT Sorry for not mentioning it, value
is of type QuerySnapshot
(firebase). Also I'm using Android's Java API 16 (which means I don't have java 8).