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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:28:21+00:00 2026-06-08T20:28:21+00:00

I just have a map implementation below to sort the values in descending order

  • 0

I just have a map implementation below to sort the values in descending order and I have used the below implementation.

public static Map<String,Integer> sortByComparator(Map<String,Integer> unsortMap) {

    List list = new LinkedList(unsortMap.entrySet());

    //sort list based on comparator
    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getValue())
                    .compareTo(((Map.Entry) (o1)).getValue());
        }
    });

    //put sorted list into map again
    Map sortedMap = new LinkedHashMap();
    for (Iterator it = list.iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry)it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}   

This does the necessary functions.. But I am curious to know whether I will be able to use tree map to sort this by descending order

  • 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-08T20:28:24+00:00Added an answer on June 8, 2026 at 8:28 pm

    You won’t, as the TreeMap sorts by key order, and you are sorting by value.

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

Sidebar

Related Questions

I have a relatively complicated generic type (say Map<Long,Map<Integer,String>> ) which I use internally
Macromedia Flash 8 Question: I have a map of Ireland, just a gif, and
I just started working with Android and I have a problem with my map.
I just have a test. Config jndi in $CATALINA_HOME/conf/context.xml like below: <Resource name=jdbc/db auth=Container
Is there some implementation of java.util.Map that does not uses HashCode? I have the
I have just started to look at the Scala collections library re-implementation which is
I have ID values of the type unsigned int . I need to map
I'm looking for an on-disk implementation of java.util.Map . Nothing too fancy, just something
I have a Map with my data and want to build a query string
Suppose I have the below class. class S{ String txt = null; S(String i){

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.