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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:53:50+00:00 2026-06-05T20:53:50+00:00

This is my hashmap : Map<String,String> unsortMap = new HashMap<String,String>() this map contains values

  • 0

This is my hashmap :

Map<String,String> unsortMap = new HashMap<String,String>() this map contains values like follows

unsortMap.put("18/06/2012", "18/06/2012");
unsortMap.put("19/06/2012", "19/06/2012");
unsortMap.put("20/06/2012", "20/06/2012");
unsortMap.put("26/06/2012", "26/06/2012");
unsortMap.put("27/06/2012", "27/06/2012");
unsortMap.put("04/07/2012", "04/07/2012");
unsortMap.put("13/07/2012", "13/07/2012");
unsortMap.put("29/06/2012", "29/06/2012");

the code that i use to sort this map is as follows :

package samples;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class SortMapExample{

   public static void main(String[] args) {

    System.out.println("Unsort Map......");
    Map<String,String> unsortMap = new HashMap<String,String>();

    unsortMap.put("18/06/2012", "18/06/2012");
    unsortMap.put("19/06/2012", "19/06/2012");
    unsortMap.put("20/06/2012", "20/06/2012");
    unsortMap.put("26/06/2012", "26/06/2012");
    unsortMap.put("27/06/2012", "27/06/2012");
    unsortMap.put("04/07/2012", "04/07/2012");
    unsortMap.put("13/07/2012", "13/07/2012");
    unsortMap.put("29/06/2012", "29/06/2012");

    Iterator iterator=unsortMap.entrySet().iterator();

        for (Map.Entry entry : unsortMap.entrySet()) {
            System.out.println("Key : " + entry.getKey() 
                + " Value : " + entry.getValue());
        }

        System.out.println("Sorted Map......");
        Map<String,String> sortedMap =  sortByComparator(unsortMap);

        for (Map.Entry entry : sortedMap.entrySet()) {
            System.out.println("Key : " + entry.getKey() 
                + " Value : " + entry.getValue());
        }
   }

   private static Map sortByComparator(Map 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) (o1)).getValue())
               .compareTo(((Map.Entry) (o2)).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;
   }    
}

The result i get is this:

Sorted Map......
Key : 04/07/2012 Value : 04/07/2012
Key : 13/07/2012 Value : 13/07/2012
Key : 18/06/2012 Value : 18/06/2012
Key : 19/06/2012 Value : 19/06/2012
Key : 20/06/2012 Value : 20/06/2012
Key : 26/06/2012 Value : 26/06/2012
Key : 27/06/2012 Value : 27/06/2012
Key : 29/06/2012 Value : 29/06/2012

I want the result to be like the following : (the map should be sorted based on the month also)

18/06/2012
19/06/2012
20/06/2012
26/06/2012
27/06/2012
29/06/2012
04/07/2012
13/07/2012

Pleas help me how to about this?

  • 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-05T20:53:51+00:00Added an answer on June 5, 2026 at 8:53 pm

    Convert your data to a SortedMap, specifically a TreeMap instance.

    Use the constructor that takes a Comparator and within that, do your comparison logic.

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

Sidebar

Related Questions

I have HasMap object like this: HashMap<String,String> fileCounter = new HashMap<String,String>(); fileCounter.put(Total Files,15); fileCounter.put(Total
If I have a HashMap that looks like this: HashMap<String, MyObject> where the String
The compiler complains about this code: HashMap<String,int> userName2ind = new HashMap<String,int>(); for (int i=0;
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I currently have a variable that looks like this: val someVal = new HashMap[Float,
I have set up a HashMap like so: Map<String, ArrayList<String>> theAccused = new HashMap<String,
I have the following code: class Parameterizable{ var map: Map[String, String] = new scala.collection.immutable.HashMap()
Can somebody explain me why this Map<String, List<String>> foo = new HashMap<String, LinkedList<String>>(); generates
OK so I have this HashMap private Map<String, Player> players = new HashMap<String, Player>();
OK so this is a BIT different. I have a new HashMap private Map<String,

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.