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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:44:39+00:00 2026-06-08T07:44:39+00:00

Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? I

  • 0

Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?

I am using map interface to read from a file and then store the values in that as a key value pair. The file format is as follows

 A 34
 B 25
 c 50

I will read the datas from this file and store that as a key value pair and then I will display this to the user. My requirement is to display the results in this format

C 50
A 34
B 25

Thus I need to sort the map in descending order of the value. So that I will be able to display these as my result .. I have read about this and find the below code

static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
        SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
            new Comparator<Map.Entry<K,V>>() {
                @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                    int res = e1.getValue().compareTo(e2.getValue());
                    return res != 0 ? res : 1; // Special fix to preserve items with equal values
                }
            }
        );
        sortedEntries.addAll(map.entrySet());
        return sortedEntries;
    }

I hope this is gonna sort the values in ascending order, I just want to know whether this approach is correct or some other effective approach will be helpful for me ?

  • 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-08T07:44:40+00:00Added an answer on June 8, 2026 at 7:44 am

    Since you can have duplicate values you shouldn’t be using a Set at all. Change to a List and sort it instead. Your entriesSortedByValues would look something like this:

    static <K,V extends Comparable<? super V>> 
                List<Entry<K, V>> entriesSortedByValues(Map<K,V> map) {
    
        List<Entry<K,V>> sortedEntries = new ArrayList<Entry<K,V>>(map.entrySet());
    
        Collections.sort(sortedEntries, 
                new Comparator<Entry<K,V>>() {
                    @Override
                    public int compare(Entry<K,V> e1, Entry<K,V> e2) {
                        return e2.getValue().compareTo(e1.getValue());
                    }
                }
        );
    
        return sortedEntries;
    }
    

    Note: in your example output the values is descending. If you want them ascending, use e1.getValue().compareTo(e2.getValue()) instead.


    Example:

    public static void main(String args[]) {
    
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("A", 34);
        map.put("B", 25);
        map.put("C", 50);
        map.put("D", 50); // "duplicate" value
    
        System.out.println(entriesSortedByValues(map));
    }
    

    Output:

    [D=50, C=50, A=34, B=25]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? Here's
Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? Dear,
Possible Duplicate: Using Perl, how can I sort an array using the value of
Possible Duplicate: Python: Sort a dictionary by value I need to sort by values
Possible Duplicate: How to write Python sort key functions for descending values In Python
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: how to sort a multidemensional array by an inner key How to
Possible Duplicate: HTML5 local storage sort I am storing data into a localstorage using
Possible Duplicate: Sort by key of dictionary inside a dictionary in Python I have
Possible Duplicate: Python analog of natsort function (sort a list using a “natural order”

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.