I've actually got this working from this example:
static <K,V extends Comparable<? super V>>
List<Entry<K, V>> entriesSortedByValues(Map<K,V> map) {
List<Entry<K,V>> sortedEntries = new ArrayList<Entry<K,V>>(map.entrySet());
Collections.sort(sortedEntries,
new Comparator<Entry<K,V>>() {
@Override
public int compare(Entry<K,V> e1, Entry<K,V> e2) {
return e2.getValue().compareTo(e1.getValue());
}
}
);
return sortedEntries;
}
I found it on this site; here.
Now, I have implemented this code and it does work. I understand what's happening, for the most part. The only part of the code I don't fully understand is within the method header, specifically:
static <K,V extends Comparable<? super V>>
If anybody could explain this part of the code to me, I'd be very grateful. Specifically why I should use super in this context.
I'm fairly new to Java so trying to get a good understanding of using different techniques rather than just throwing them into my code without really understanding what's happening.
Note: I would have commented on the original answer but I can only leave comments if I have a reputation over 50. I hope I'm not breaking any rules by posting like this!