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 9241213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:19:52+00:00 2026-06-18T08:19:52+00:00

I have a map: TreeMap<String, Integer> m = new TreeMap<>(); where I have a

  • 0

I have a map: TreeMap<String, Integer> m = new TreeMap<>(); where I have a whole alphabet and values, which shows how many times each letter was found in my text.

I want to sort that map in descending count order; that is, the most frequent letter is on the first line, and the last line of output indicates the least frequent letter. If two letters have the same frequency, then the letter which comes first in the alphabet must appear first. How to make it?

I tried with Comparator:

public int compare(String a, String b) {
        if (base.get(a) >= base.get(b) && a.compareToIgnoreCase(b) < 0) {
            return -1;
        } else {
            return 1;
        }
    }

but still, its not it, the output is:

D 3
E 3
A 2
S 5

Guys … Found this before, this didnt help at all. Good output should be:

S 5
D 3
E 3
A 2
  • 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-06-18T08:19:53+00:00Added an answer on June 18, 2026 at 8:19 am

    Your comparator does not look right – this should work better:

    public int compare(String a, String b) {
        if (base.get(a) > base.get(b)) {
            return -1;
        } else if (base.get(a) < base.get(b)) {
            return 1;
        } else {
            int stringCompare = a.compareToIgnoreCase(b);
            return stringCompare == 0 ? 1 : stringCompare; // returning 0 would merge keys
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have following map: NavigableMap<Integer,String> nmap = new TreeMap<Integer,String>(); nmap.put(3, Three); nmap.put(1, One);
I have a TreeMap resMap new TreeMap<String, Map<String, String>>(); I would like to filter
I have the following TreeMap: TreeMap<String, Integer> distances = new TreeMap<String, Integer>(); and it
I have these two data structures: public TreeMap<String, Integer> tableFrequency; public SortedSet<Map.Entry<String, Integer>> sortedTable;
I have the following Map: Map<String, List<String>> map = new HashMap<String, List<String>>(); which is
I have a TreeMap defined like: final TreeMap<ArrayList<String>,String> mymap = new TreeMap<ArrayList<String>,String>(comparator); When I
I need a Map<Integer,String> with a major need to do fast retrievals of values
I have a Map[Long, String] which I would like iterate over in descending order
How does the TreeMap sort? say for example you have the following map: TreeMap<String,
I was looking for ways of sorting Map<String, Integer> by values. I found this

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.