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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:10:38+00:00 2026-06-16T08:10:38+00:00

I have an ArrayList<HashMap<String,String>> . Is there a way to change the order of

  • 0

I have an ArrayList<HashMap<String,String>>.

Is there a way to change the order of the array of HashMaps? For example, if the String “firstName” is inside the HashMap, can I sort my ArrayList of HashMaps by that value? Just to be clear, I do not need to order keys and objects inside HashMaps. I need order HashMaps inside Arraylist.

What is a best way?

i tried to do from examples:

class MapComparator implements Comparator<HashMap<String, String>>
{
    private final String key;

    public MapComparator(String key)
    {
        this.key = key;
    }

    public int compare(HashMap<String, String> first,
            HashMap<String, String> second)
    {
        // TODO: Null checking, both for maps and values
         String firstName1 = first.get(key);
            String firstName2 = second.get(key);
            if(firstName1 == null)
                if(firstName2 == null)
                    return 0;
                else
                    return -1; // treat null as less than any non-null
            else
                if(firstName2 == null)
                    return 1; // treat null as less than any non-null
                else
                    return firstName1.compareTo(firstName2);

    }
}

and using:

                Log.e(THIS_FILE, "before ->" + addedRows);
                Collections.sort(addedRows, new MapComparator("name"));
                Log.e(THIS_FILE, "after ->" + addedRows);

and nothing sorted (to be sure i renamed one contact to start by letter a, hashmap with name=as ice:) must be first):

> E/ContactsActivity(23571): before ->[{type=, contactID=4, name=Office
> O}, {type=, contactID=2912, name=Test Text Last}, {type=,
> contactID=2915, name=as ife:) Eng}, {type=, contactID=2914,
> name=life:) Rus}, {type=, contactID=2913, name=life:) Ukr}, {type=,
> contactID=2897, name=дима куплеватскиц}] E/ContactsActivity(23571):
> after ->[{type=, contactID=4, name=Office O}, {type=, contactID=2912,
> name=Test Text Last}, {type=, contactID=2915, name=as ife:) Eng},
> {type=, contactID=2914, name=life:) Rus}, {type=, contactID=2913,
> name=life:) Ukr}, {type=, contactID=2897, name=дима куплеватскиц}]
  • 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-16T08:10:39+00:00Added an answer on June 16, 2026 at 8:10 am

    It sounds like what you want is:

    java.util.Collections.sort
    (
        list,
        new java.util.Comparator<Map<String,String>>()
        {
            public int compare(Map<String,String> map1, Map<String,String> map2)
            {
                String firstName1 = map1.get("firstName");
                String firstName2 = map2.get("firstName");
                if(firstName1 == null)
                    if(firstName2 == null)
                        return 0;
                    else
                        return -1; // treat null as less than any non-null
                else
                    if(firstName2 == null)
                        return 1; // treat null as less than any non-null
                    else
                        return firstName1.compareTo(firstName2);
            }
        }
    );
    

    (where list is the name of your variable of type ArrayList<HashMap<String,String>>).

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

Sidebar

Related Questions

I have this HashMap<String, ArrayList<Item>> , is there a way to count the total
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have the following ArrayList ArrayList<HashMap<String, String>> list; HashMap<String, String> map; with the following
I have set up a HashMap like so: Map<String, ArrayList<String>> theAccused = new HashMap<String,
I have an ArrayList<HashMap<String, String>> . I'd like to quickly extract from this a
I have created an ArrayList<HashMap<String, String>> collection to hold my data for ListView .
Say I have a hashmap with String type as key and ArrayList type as
I have HashMap like: static HashMap<String,ArrayList<Media>> mediaListWithCategory=new HashMap<String,ArrayList<Media>>(); I have value like: January: -Sunday
I have a HashMap<String,String> and there is a static method which returns this map
I have a List of HashMap such as below ArrayList l = new ArrayList

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.