Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:07:53+00:00 2026-05-29T08:07:53+00:00

This has been asked several times, i know, but help me understand something. You

  • 0

This has been asked several times, i know, but help me understand something.

You have a map you need sorted by Value

    Map<String, Integer> m = new HashMap<String, Integer>();
    m.put("a", 1);
    m.put("b", 13);
    m.put("c", 22);
    m.put("d", 2);

You call a method to make it happen

public static List<String> sortByValue(final Map<String, Integer> unsortedMap) {

    List<String> sortedKeys = new ArrayList<String>();
    sortedKeys.addAll(unsortedMap.keySet());

    Collections.sort(sortedKeys, new MapComparator(unsortedMap));

    return sortedKeys;
}

You have a comparator class

public MapComparator(Map<String, Integer> m) {
    this.m = m;
}

@Override
public int compare(String a, String b) {

    int x = m.get(a);
    int y = m.get(b);

    if (x > y)
        return x;
    if (y > x)
        return y;

    return 0;

}

This code, obviously is flawed. Please help me understand why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:07:54+00:00Added an answer on May 29, 2026 at 8:07 am
      if (x > y)
        return x;
      if (y > x)
        return y;
    
      return 0;
    

    You should be returning 1 if x > y and -1 if y > x. The Comparator contract specifies that you return a negative number if the first value is less than the second, a positive number if the first is greater than the second, and zero if they’re equal.

    (Mind you, as it stands, this Comparator implementation will break in very confusing ways if you ever happen to use values that aren’t in the original map.)

    Better yet, just return Integer.compare(x, y), which does all that for you. (Only in Java 7, though.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked several times for sure, but I have
I know this has been asked different ways several times, but I'm just not
I know that this question have been asked several times. But I can't get
I know this has been asked several times and I have read all the
I know this question has been asked several times, but I can't quite seem
I know this question has been asked several times on SO, but none of
I know this has been asked several times, but I searched and I tried
I know this has been asked several times, but after a few hours of
I know this has been asked several times before but I can't seem to
I know this question has been asked several times, but I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.