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

  • Home
  • SEARCH
  • 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 6346463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:01:19+00:00 2026-05-24T21:01:19+00:00

I need to sort a 2d key/value pair by the value. I have read

  • 0

I need to sort a 2d key/value pair by the value. I have read many references about this on the web and have ended up writing my own class to do this using HashMaps(see below). I put the code into a condensed working class that reproduces the problem with the minimum amount of code so that you can just cut and paste it into your IDE for quick diagnostics.

As you can see, the method that I wrote is correctly sorting the values before they are entered into sortedMap. However, for some reason, they values are then again unordered in a different way when I try to subsequently iterate through sortedMap.

Can anyone show me how to fix the code below so that I iterate through a resulting 2D data object that gives me my data in descending order?


EDIT: Re-wrote using TreeMaps, and am still getting a similar problem. Here is the re-written code:

import java.util.*;

public class HashMapDemo {
    public static void main(String args[]) {

        // Code that creates and populates the unordered HashMap:
        TreeMap<Integer, Double> unSortedMap = new TreeMap<Integer, Double>();
        unSortedMap.put(1343, 0.521851);
        unSortedMap.put(1950, -0.301208);
        unSortedMap.put(3667, -0.0280762);
        unSortedMap.put(3879, 0.154724);
        unSortedMap.put(4124, 0.022583);

        // Code that calls the ordering method:
        TreeMap<Integer, Double> sortedMap = new TreeMap<Integer, Double>(
                sortTreeMap(unSortedMap));

        // Code that iterates through the "sorted" hashmap.
        System.out.println("now iterate through sortedMap: ");
        for (Integer key : sortedMap.keySet()) {
            System.out.println("key, sortedMap.get(key) are:  " + key + ", "
                    + sortedMap.get(key));
        }
    }

    // Code for the ordering method. Note that the println tests indicate that
    // this method is correctly sorting the key/value pairs in the hashmap:
    private static TreeMap<Integer, Double> sortTreeMap(
            TreeMap<Integer, Double> input) {

        System.out
                .println("input.size() upon entering sortHasMap() function is: "
                        + input.size());
        int startSize = input.size();

        // create a hashmap to store sorted output
        TreeMap<Integer, Double> sortedMap = new TreeMap<Integer, Double>();

        // repeat the following process once for every key/value pair in the
        // hashmap
        for (int i = 0; i < startSize; i++) {
            int mySize = input.size();
            System.out.println("mySize is: " + mySize);
            double maxVal = Double.NEGATIVE_INFINITY;
            int maxKey = 0;

            // iterate through each key in hashmap to find key/value of max
            // value
            for (Integer key : input.keySet()) {
                if (maxVal < input.get(key)) {
                    maxVal = input.get(key);
                    maxKey = key;
                }
            }

            // add key/value of max of that iteration to sorted map and remove
            // from input before next iteration
            sortedMap.put(maxKey, maxVal);

            input.remove(maxKey);

            System.out.println("sortedMap.put(maxKey, maxVal) are: " + maxKey
                    + ", " + maxVal);
        }
        return sortedMap;
    }
}
  • 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-24T21:01:21+00:00Added an answer on May 24, 2026 at 9:01 pm

    You need only two lines to achieve what you want. Here are those two lines:

        Map<Integer, Double> sortedMap = new TreeMap<Integer, Double>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return map.get(o2).compareTo(map.get(o1)); // reverse order of values
            }
        });
        sortedMap.putAll(map);
    

    Here’s the complete runnable code:

    public static void main(String[] args) {
        final Map<Integer, Double> map = new HashMap<Integer, Double>();
        map.put(1343, 0.521851);
        map.put(1950, -0.301208);
        map.put(3667, -0.0280762);
        map.put(3879, 0.154724);
        map.put(4124, 0.022583);
        Map<Integer, Double> sortedMap = sortMap(map);
    
        for (Map.Entry<Integer, Double> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey() + ", " + entry.getValue());
        }
    }
    
    public static Map<Integer, Double> sortMap(final Map<Integer, Double> map) {
        Map<Integer, Double> sortedMap = new TreeMap<Integer, Double>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return map.get(o2).compareTo(map.get(o1));
            }
        });
        sortedMap.putAll(map);
        return sortedMap;
    }
    

    Output:

    1343, 0.521851
    3879, 0.154724
    4124, 0.022583
    3667, -0.0280762
    1950, -0.301208
    

    Notes: You specify how you want entries ordered in a TreeSet by passing the Comparator you want to use into the constructor. The TreeSet implementation does the rest.

    Other notes:

    • The best way to iterate over the keys/values of a map is by iterating over Map.entrySet()
    • Always use the abstract type for your variables – eg Map<?, ?> myMap not the concrete implementation (ie HashMap<?, ?> myMap)

    Here’s the generic version of the sortMap method that will sort any suitable map based on the reverse order of the values:

    public static <K, V extends Comparable<V>> Map<K, V> sortMap2(final Map<K, V> map) {
        Map<K, V> sortedMap = new TreeMap<K, V>(new Comparator<K>() {
            public int compare(K o1, K o2) {
                return map.get(o2).compareTo(map.get(o1));
            }
        });
        sortedMap.putAll(map);
        return sortedMap;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a the need for key value pair that I wish to sort
I need some sort of way to store key/value pairs where the value can
I need to read a set of files and break it in to key-value
I need to sort an std::map by value rather than by key. Is there
I have the following xml and I need to sort this xml using XSLT
I need to arrange sort of dictionary where the key would be a pair
I have an array of objects with several key value pairs, and I need
I need some sort of interactive chart control for my .NET-based web app. I
I need to sort an array that can look like this: $array[4][0] = array('id'
I have a gridview and I need to sort its elements when the user

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.