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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:40:17+00:00 2026-05-25T23:40:17+00:00

I have an ArrayList . Each element is a HashMap lookup for my values.

  • 0

I have an ArrayList.

Each element is a HashMap lookup for my values.

1 of the values is numeric.

I’m using Collections.sort() for alpha sorting. But the numbers are getting alpha sorted, instead of numeric sorted.

protected ArrayList<HashMap<String, String>> myAccountList;

String Name = myAccountList.get(i).get("accountName");
Double balance = Double.valueOf(myAccountList.get("accountBalance"));  //is a number stored as a string

So I have the sort working on accountName just fine.

Collections.sort(myAccountList, new Comparator(){
   public int compare(Object o1, Object o2) {
      HashMap<String,String> old1 = (HashMap<String, String>) o1;
      HashMap<String,String> old2 = (HashMap<String, String>) o2;
      return old1.get("accountName").compareToIgnoreCase(old2.get("accountName"));
   }
});

How would I sort the field accountBalance as a number? I doubt it matters, but I’m programming an Android app.

  • 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-25T23:40:18+00:00Added an answer on May 25, 2026 at 11:40 pm

    It looks like you’re using a HashMap where you should design your own class:

    public class Account {
    
       public final String name;
       public final Double balance;
    
       public Account(String name, Double balance) {
          this.name = name;
          this.balance = balance;
       }
    }
    

    (Note: currently this class is immutable. To make it mutable, change the fields from public final to private and add getters/setters)

    Then you can store them in a List<Account> and sort accordingly:

    //sort by name
    Collections.sort(myAccountList, new Comparator<Account>(){
       public int compare(Account acc1, Account acc2) {
          return acc1.name.compareTo(acc2.name);
       }
    });
    
    //sort by balance
    Collections.sort(myAccountList, new Comparator<Account>(){
       public int compare(Account acc1, Account acc2) {
          return acc1.balance.compareTo(acc2.balance);
       }
    });
    

    Note the use of a Comparator with the generic type parameter <Account>, so you don’t have to cast the compareTo() arguments from Object.

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

Sidebar

Related Questions

I have an Arraylist of HashMap . Each HashMap element contains two columns: column
I have an arraylist that contains items called Room. Each Room has a roomtype
I have an arraylist that gets different type of values in it, 1st value->
I have a List containing HashMaps . Each HashMap in the list might have
I want to add multiple BigInteger values to an ArrayList . All I have
I have a HashMap called examList which stored exam grades of each course a
I have two arraylists A1, A2. Each element of A1 would be the key
i have two arrays (actually one, but i created two for each columns). I
I have a problem of adding elements into an ArrayList. Each time I do
I have an ArrayList<Double> , the values of which are converted into slices of

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.