Sort HashMap By Value Java

Sort HashMap By Value Java

This question is asked in many interview to check candidate ability to write logic quickly. Most of the time candidate fails:

  • Output sort in natural order:

  • Now if you want to sort above HashMap in reverse order then just putt “-” or negative sign in-front of your return:

return (-o1.getValue().getFirstName().compareTo(o2.getValue().getFirstName()));

  • Complete program:

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *