I have an Author object with name, age, and index fields. The Authors are stored in a HashMap (the key is the index).
I would like to be able to sort by name and age.
This begins a method I want to put in my Author class but clearly I am not understanding this:
public static LinkedHashMap<String, Author> sortName(Map<String, Author> unSortedMap){
Collection<Author> list = unSortedMap.values();
Collections.sort(list, new Comparator<Author>() {
public int compare(Author a, Author b) {
return a.name.compareTo(b.name);
}
});
}
It won't compile because of this: (from Eclipse)
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Collection<Author>, new Comparator<Author>(){})
The Collections class fields are EMPTY_LIST, EMPTY_MAP, and EMPTY_SET. Why can't the sort method pass a Map with a Comparator?
I thought
<T>was generic so I could pass any kind of object I wanted.Am I correct that if this worked, Collections.sort() would sort
listby the Author's name? So I would end up with aLinkedList<Author>that I could turn into aLinkedHashMap<String, Author>and send on it's way?
Thank you in advance. Clearly I'm student. I've also searched for HOURS for information that could guide me toward a solution. I've read every question re: "Collections sort multiple values comparator". Nothing is working.