Add accounts on a list, each account contain: name, accountCode, pinCode, balance.
How to show list sort by balance?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Add accounts on a list, each account contain: name, accountCode, pinCode, balance.
How to show list sort by balance?
Have a look at Comparator and Comparable.
nhat.tv18 (March 26th, 2014)
I assumed that you have your Accounts class that contain name, accountCode, pinCode, balance.
You can use the sort method of Collections under java.util class. Collections (Java Platform SE 7 )
But it would cause a compilation error, (if Comparable interface did not implement by your Accounts class)
so to do that, you implement first the Comparable interface under java.lang in your Accounts class Comparable (Java Platform SE 7 ) then implement its abstract method compareTo then do the comparison of balance. (Make sure to make your Comparable class generic to Accounts class if you are using java 6 or 7 so don't need to cast it)
you can also use Comparator interface when sorting. But I think Comparable would be better
nhat.tv18 (March 26th, 2014)
Thank all
Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.
nhat.tv18 (March 26th, 2014)