I need some help how to sort an ArrayList of objects. I have the superclass Account and two subclasses SavingsAccount and CreditAccount. Inside the Account class I have this method to call when I want to know the account number:
// Get account number
public String getAccountNumber() {
return accountNumber;
}
I need to sort the account numbers to get the highest number of all accounts in the objects?
The ArrayList is like this:
ArrayList<Account> accountList = new ArrayList<Account>();
Could this be done in a simple and not to comlicated way? Thanks!
You can implement
Comparable
interface in yourAccount
class. And then usejava.util.Collections.sort(list)
method.}
For a start, why is an account number being represented as a string? Is it a number, or is it text? Anyway, it's absolutely possible to sort your list using
Collections.sort
:If that sorts them in the wrong sense for you (ascending instead of descending), reverse the comparison:
Note that as these are strings, it will sort them in lexicographic order. If you were using numbers instead, that wouldn't be a problem.
An alternative which I wouldn't recommend in this case is to make
Account
implementComparable<Account>
. That's suitable when there's a single "natural" way of comparing two accounts - but I can see that you might want to sometimes sort by number, sometimes by the name of the account holder, sometimes by the funds within the account, sometimes by the date the account was created etc.As an aside, it's not clear that you really need to sort this at all - if you just need to find the largest account number, you don't need to sort it, you just need to iterate and remember the account with the largest number as you go:
will work if
Account implements Comparable<Account>
. If not, you need to pass in a comparatorFrom the javadoc:
I'm sorry to say your question is not very clear. What you mean by "I need to sort the account numbers to get the highest number of all accounts in the objects?" I guess you need to sort your ArrayList on the basis of accountNumber which is a String!!
To sort any objects in Java, they should implement the simple interface Comparable. Most of the Java built-in classes like String,Date etc already implement Comparable, and thats why we are able to sort them. For OUR classes WE should take the pain to tell the JVM on what basis it should sort OUR objects. You can tell this for Account class as shown below
Now simply call Collections.sort(accountList).
Below is the sample test code and result
This printed
[Account [accountNumber=1FF], Account [accountNumber=AA1], Account [accountNumber=AA2], Account [accountNumber=BB1]]
ie in the decreasing alphabetical order. If you want the other way around (increasing order of alphabets) you can either call Collections.reverse(accountList) or go and change the compareTo() implementation as below.
Sometimes you may be in a position where you cannot accomplish this using Comparable interface (may be because you don't have the source for that class or it is already implementing Comparable interface to sort on the basis of another attribute, say bank balance!). In this case you need to create a Comparator as below.
Now simply call
Advantage of this approach is you can create as many comparators you want like AccountBalanceComparator,AccountNameComparator etc on basis of different attributes, which can then be used to sort your list in different ways as you wish.
Use
Collections.sort(accountList );
to sort theArrayList
java.util.Collections.sort(list)
- use it when Account implements Comparable.