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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:52:13+00:00 2026-06-01T00:52:13+00:00

I have a Map to sort as follows: Map<String, String> map = new HashMap();

  • 0

I have a Map to sort as follows:

Map<String, String> map = new HashMap(); 

It contains the following String keys:

String key = "key1.key2.key3.key4" 

It contains the following String values:

String value = "value1.value2"

where the key and value can vary by their number of dot sections from key1/value1 to key1.key2.key3.key4.key5/value1.value2.value3.value4.value5 non-homogeneously

I need to compare them according to the number of dots present in keys or in values according to the calling method type key / value :

sortMap(Map map, int byKey);

or

sortMap(Map map, int byValue);

The methods of course will return a sorted map.

Any help would be appreciated.

  • 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-01T00:52:14+00:00Added an answer on June 1, 2026 at 12:52 am

    For starters, you will need to be using an instance of SortedMap. If the map doesn’t implement that interface, then it has an undefined/arbitrary iteration order and you can’t control it. (Generally this is the case, since a map is a way of associating values with keys; ordering is an auxiliary concern.)

    So I’ll assume you’re using TreeMap, which is the canonical sorted map implementation. This sorts its keys according to a Comparator which you can supply in the constructor. So if you can write such a comparator that determines which is the “lower” of two arbitrary keys (spoiler alert: you can), this will be straightforward to implement.

    This will, however, only work when sorting by key. I don’t know if it makes much sense to sort a map by value, and I’m not aware of any straightforward way to do this. The best I can think of is to write a Comparator<Map.Entry> that sorts on values, call Map.getEntrySet and push all the entries into a list, then call Collections.sort on the list. It’s not very elegant or efficient but it should get the job done if performance isn’t your primary concern.

    (Note also that if your keys aren’t immutable, you will run into a lot of trouble, as they won’t be resorted when externally changed.

    • 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 have a map declared as follows: map < string , list < string
How to sort (any kind of sorting) a key of a map(treemap or hashmap)
I have a problem with HashMap. I want to put into map: key -
I have a Map that uses a Set for the key type, like this:
Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? Here's
Can a STL map be used for keys of varying sizes? I don't have
I have some thing like this code: map<int, string> m; m[1] = a; m[2]
I have a map where both the keys and values are generic types. Something
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>

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.