I have a HashMap of String, RetentionFeedUser.
The RetentionFeedUser is an object with 2 int values, which I want to sort the hashmap by both of them. Here is my CompareTo method inside RetentionFeedUser class -
@Override
public int compareTo(RetentionFeedUser other) {
//returns 1 if bigger, 0 if equal and -1 if smaller
return this.videoCounter - other.videoCounter + this.winningCounter - other.winningCounter;
}
here is my actual sorting method I use in my Activity -
private void sortHashMap(Map<String, RetentionFeedUser> retentionFeedUserHashMap) {
List<Map.Entry<String, RetentionFeedUser>> list = new ArrayList<>(retentionFeedUserHashMap.entrySet());
Collections.sort(list, (entry, t1) -> entry.getValue().compareTo(t1.getValue()));
Collections.reverse(list);
}
for some reason, it sorts the user in a weird way, giving me only even numbers for the inner RetentionFeedUsers which I know is incorrect. The second thing I notice is that in the log cat it does sort it correctly (even though the values are incorrect, it follows the pattern I want) but up to a certain point. I will demonstrate -