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.

Results 1 to 5 of 5

Thread: How to sort a list by balance?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to sort a list by balance?

    Add accounts on a list, each account contain: name, accountCode, pinCode, balance.
    How to show list sort by balance?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How to sort a list by balance?

    Have a look at Comparator and Comparable.

  3. The Following User Says Thank You to PhHein For This Useful Post:

    nhat.tv18 (March 26th, 2014)

  4. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: How to sort a list by balance?

    Quote Originally Posted by nhat.tv18 View Post
    Add accounts on a list, each account contain: name, accountCode, pinCode, balance.
    How to show list sort by balance?
    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

  5. The Following User Says Thank You to dicdic For This Useful Post:

    nhat.tv18 (March 26th, 2014)

  6. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to sort a list by balance?

    Thank all

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to sort a list by balance?

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    nhat.tv18 (March 26th, 2014)

Similar Threads

  1. Bubble Sort on double Linked List
    By Wolverine89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2013, 12:44 PM
  2. Sort and Merge Two Linked List
    By Loraeron in forum What's Wrong With My Code?
    Replies: 49
    Last Post: February 28th, 2013, 10:08 PM
  3. Help with using mergesort to sort a list of names alphabetically?
    By BearBearBear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2012, 09:10 AM
  4. Insertion Sort on a Doubly Linked List
    By Kaisshau in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 7th, 2011, 11:18 PM
  5. Pseudo code of insertion sort linked list
    By sungirl in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 23rd, 2010, 09:25 AM