2

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.

4
  • Why not store balances as a Double instead? For example, HashMap<String, Double> Commented Sep 30, 2011 at 1:30
  • OK, I can do that. But what would the sort look like? And how do I get the HashMap to accept <String, String> as well as <String, Double> ?? Commented Sep 30, 2011 at 1:32
  • You can't do that if you're holding strings as well as doubles in the map. You could however use a HashMap<String, Object>. Commented Sep 30, 2011 at 1:36
  • @DonRoby is right. Don't use a HashMap at all - see my answer. Commented Sep 30, 2011 at 1:41

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You will have to do something like this:

Double.valueOf(old1.get("accountBalance")).compareTo(Double.valueOf(old2.get("accountBalance")));

To sort by both the account name and balance, you would check if the first compare is 0 (they are equal) and if so, return the accountBalance compare, assuming you are sorting by account name first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.