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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:01:11+00:00 2026-06-02T16:01:11+00:00

I am trying to sort a TreeMap (having Double as a value and Integer

  • 0

I am trying to sort a TreeMap (having Double as a value and Integer value as a key) using Comparator interface but it’s not working.

// Create a tree map
        TreeMap tm = new TreeMap();
        // Put elements to the map
        tm.put(1, new Double(3434.34));
        tm.put(0, new Double(123.22));
        tm.put(4, new Double(1378.00));
        tm.put(2, new Double(99.22));
        tm.put(3, new Double(-19.08));
        List<Map.Entry> valueList = new ArrayList(tm.entrySet());

        // Collections.sort(valueList, new Sort());

        Collections.sort(valueList, new Sort());

        HashMap sortedMap = new HashMap();

        // Get an iterator
        Iterator<Map.Entry> i = valueList.iterator();

        // Display elements
        while (i.hasNext()) {
            Map.Entry object = i.next();
            sortedMap.put(object.getKey(), object.getValue());
        }
        List sortedList = new ArrayList(sortedMap.entrySet());
        Iterator<Map.Entry> iterator = sortedList.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            System.out.println("Value " + entry.getValue() + "\n");
        }

The following is my Comparator class

public class Sort implements Comparator<Map.Entry> {

    @Override
    public int compare(Map.Entry o1, Map.Entry o2) {
        // TODO Auto-generated method stub
        double valueOne = (Double) o1.getValue();
        double valueTwo = (Double) o2.getValue();

        int returnValue =
            valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);

        return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
    }

}

But i am getting the following output

Value 123.22

Value 3434.34

Value 99.22

Value -19.08

Value 1378.0

Edited Part

public int compare(Map.Entry o1, Map.Entry o2) {
        // TODO Auto-generated method stub
        double valueOne = ((Double) o1.getValue()).doubleValue();
        double valueTwo = ((Double) o2.getValue()).doubleValue();

        int returnValue =
            valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);

        return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
    }
  • 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-02T16:01:14+00:00Added an answer on June 2, 2026 at 4:01 pm

    Adding to what others have already suggested, try this:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.TreeMap;
    
    public class SortDemo
    {
      public class Sort implements Comparator<Map.Entry>
      {
        public int compare(Entry o1, Entry o2)
        {
          Double valueOne = (Double) o1.getValue();
          Double valueTwo = (Double) o2.getValue();
          return (int) Math.signum(valueOne.compareTo(valueTwo));
        }
      }
    
      public static void main(String[] args)
      {
        new SortDemo().foo();
      }
    
      void foo()
      {
        TreeMap tm = new TreeMap();
        tm.put(1, new Double(3434.34));
        tm.put(0, new Double(123.22));
        tm.put(4, new Double(1378.00));
        tm.put(2, new Double(99.22));
        tm.put(3, new Double(-19.08));
    
        List<Map.Entry> valueList = new ArrayList(tm.entrySet());
        Collections.sort(valueList, new Sort());
    
        Iterator<Map.Entry> iterator = valueList.iterator();
        while (iterator.hasNext())
        {
          Map.Entry entry = iterator.next();
          System.out.println("Value: " + entry.getValue());
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a list using delegates but I am getting a
just trying to sort out a small delimma I'm having here. Currently, I'm working
I am trying to sort a TreeMap on key. Key is some custom DataStructure
I am trying to sort a dict based on its key and return an
I am trying to sort but there is a nil. How can i get
I'm trying to sort any array with array_multisort() and everything is working great. However,
I am trying to add the key values from a Treemap to a String[],
Im trying to sort through an arraylist of objects by a particular value within
I am trying to sort an array of integers in MIPS using bubble sort
I am trying to sort a list of records that have been created using

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.