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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:18:05+00:00 2026-05-24T05:18:05+00:00

I have a TreeMap that holds the following keys/values: private ArrayList<TreeMap<String,String>> mList = new

  • 0

I have a TreeMap that holds the following keys/values:

    private ArrayList<TreeMap<String,String>> mList = new ArrayList<TreeMap<String,String>>();
    TreeMap<String,String> item = new TreeMap<String,String>();
    item.put("FirstName", strFirstName);  
    item.put("LastName", strLastName);
    item.put("Country", strCountry);
    mList.add( item );

How can I sort the TreeMap by the values stored under the “LastName” key after adding all the items? I would like to have a TreeMap in the same format as the original list but with the mentioned sorting applied:

“James”, “Amish”, “USA”
“Charles”, “Brentwood”, “USA”
“Jake”, “Cornell”, “USA”
“Amy”, “Dunn”, “USA”
“Melinda”, “Ellis”, “Canada”

I’ve found a similar question relating to sorting a TreeMap by values in ‘regular’ Java, but the code didn’t work for me – I don’t know if because of Android pecularities or my inability to adapt it to fit my needs :-/

Thanks,
Nick

  • 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-24T05:18:07+00:00Added an answer on May 24, 2026 at 5:18 am

    It sounds like you are trying to sort the ArrayList containing the TreeMaps, correct?
    If so, the following should suit your needs:

    Collections.sort(mList, new Comparator<TreeMap<String, String>>(){
        public int compare(TreeMap<String, String> left, TreeMap<String, String> right) {
            return left.get("LastName").compareTo(right.get("LastName"));
        }
    });
    

    It should be noted that the use of a TreeMap to contain a list of name/value pairs is probably not necessary – I would just create a class to hold the values.

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

Sidebar

Related Questions

I have a TreeMap that maps String keys to a custom City class. Here
I have the following TreeMap: TreeMap<String, Integer> distances = new TreeMap<String, Integer>(); and it
I have created a TreeMap like so: TreeMap<Integer, ArrayList<MyClass>> wrap = new TreeMap<Integer, ArrayList<MyClass>>();
I have a method that gets a SortedMap as input, this map holds many
Have you managed to get Aptana Studio debugging to work? I tried following this,
Well, I tested TreeMap but it doesn't take in account IgnoreCase on string comparision.
I have a Java program that stores a lot of mappings from Strings to
I have a class MyMap which wraps TreeMap. (Say it's a collection of dogs
In my code I have a map that is used heavily, several thousand times
I need to have a UserProfile class that it's just that, a user profile.

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.