0

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 -

Logcat that shows it is sorted correctly but starts from 1 after that

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
  • You do realize that you are sorting a `List` local to the `sortHashMap` method, and not the `HashMap` itself, right? – Eran Aug 05 '19 at 12:38
  • Your list copies the entries from the map at initialization, then is sorted and reversed, and then that list is discarded. No modification is made to the map at any stage. You cannot sort a `HashMap`. Its implementation is written in such a way - for performance reasons - that means it is not possible. – Michael Aug 05 '19 at 12:39
  • hey @Michael, if so is true than why do I get the top results and than the bottom results over and over? – Alon Shlider Aug 05 '19 at 12:50
  • and why do all values even numbers while some are actual odd numbers? they jump in 2 numbers which is not like that – Alon Shlider Aug 05 '19 at 12:51
  • I don't understand the question, sorry. But regardless of the output, you are not sorting the map. – Michael Aug 05 '19 at 12:52
  • @Michael that is fine, I need a sorted list at the end in order to implement the content inside a Recycler View. My question is, as you can see, I am getting weird values when printing to the log the sorted list - I get to the max counted uploaded videos and then back to 1 over and over and over again and I can't realise why it is happening. – Alon Shlider Aug 05 '19 at 13:01
  • @AlonShlider, your `compareTo` doesn't look right to me. What are you trying to do, say, in SQL terms? `order by videoCounter, winningCounter`? – M. Prokhorov Aug 06 '19 at 12:05
  • @M.Prokhorov I want to order first by the videoCounter and afterwards , if the videocounter is equal, to order by won contest. but even if I have an object with 5 uploads and 6 wins and another with 9 objects and 11 wins I want the one with 5 uploads to be higher in the list. I am actually facing a problem with this one right now – Alon Shlider Aug 06 '19 at 12:45
  • 1
    @AlonShlider, then your `compareTo` is wrong, because it orders by `videoCounter + winningCounter`. You won't be able to "brain" out of it. Write it out like you've described, but in code, or create a `Comparator c = comparingInt(t -> t.videoCounter).thenComparingInt(t -> t.winningCounter);`. Reverse if need be. – M. Prokhorov Aug 06 '19 at 13:06

0 Answers0