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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:07:43+00:00 2026-06-14T17:07:43+00:00

I want to sort a Hashmap by multiple keys, at the moment i use

  • 0

I want to sort a Hashmap by multiple keys, at the moment i use the following code

Collections.sort(ilv2, new Comparator<HashMap<String,String>>() {
        public int compare(HashMap<String, String> arg0, HashMap<String, String> arg1) {
            // TODO Auto-generated method stub
            Time d1 = Time.valueOf(arg0.get("stamp"));
            Time d2 =Time.valueOf(arg1.get("stamp"));
            return  d1.compareTo(d2);
        }
    });

Th Hasmap ilv2 is sortet by the Time key “stamp”, but now i want to sort by the key res_id (int) and then by time, like the sql statement “order by res_id, stamp”
Any suggestions?

edit:
solution based on the given answer

Collections.sort(ilv2, new Comparator<HashMap<String,String>>() {
        public int compare(HashMap<String, String> arg0, HashMap<String, String> arg1) {
            // TODO Auto-generated method stub
            int i = 0;
            int r1 = Integer.valueOf(arg0.get("resid"));
            int r2 = Integer.valueOf(arg1.get("resid"));
            i = r1-r2;
            if(i==0){
                Time d1 = Time.valueOf(arg0.get("stamp"));
                Time d2 =Time.valueOf(arg1.get("stamp"));
                 i = d1.compareTo(d2);
            }
            return  i;
        }
    });
  • 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-14T17:07:44+00:00Added an answer on June 14, 2026 at 5:07 pm

    From my code, something like this:

    public int compare(ProductItemData pr1, ProductItemData pr2) {
                    int i = pr1.crossed.compareTo(pr2.crossed);
                    if(i==0) {
                        int j = pr1.addTime.compareTo(pr2.addTime);
                        if(j==0)
                            return ((Long)pr1.id).compareTo(pr2.id);
                        else
                            return j;
                    }
                    return i;
                }
    

    Read about what compareTo return.

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

Sidebar

Related Questions

I have the following Map: Map<String, List<String>> map = new HashMap<String, List<String>>(); which is
I want to sort multiple and multilevel unordered lists. An example: <!DOCTYPE html> <html>
I am new to java. I want to sort a concurrent hash map by
How can I sort HashMap keys by their numerical value? Currently, in the natural
I want to sort an Hashmap by the object's value. In this case, by
I have a HashMap<String, Integer> How can I sort this data structure and keep
Basically, I have an arraylist declared like : private ArrayList<HashMap<String, String>> songsList = new
I want to sort a mutable array by date. My array contains several dict
I want to sort the items that have been previously selected with checkboxes. If
I want to sort items in ListBox in aspx.net. I'm binding Datasource to my

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.