Solved

Sort a list

Posted on 2007-11-14
5
201 Views
Last Modified: 2010-03-30
Hi All,

I have a list of objects in random order.

The object details are as follows :

String ID
String totalAmount

How can I order by totalamount so that the object with greatest total amount appears at the beginning of the list and so on.

Thanks
0
Comment
Question by:ip_packet
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:ysnky
Comment Utility
0
 
LVL 9

Expert Comment

by:ysnky
Comment Utility
0
 
LVL 8

Accepted Solution

by:
mnrz earned 325 total points
Comment Utility
use following code

Collections.sort(yourList, new Comparator<MyObject>() {

    public int compare(MyObject o1, MyObject o2){
             if(o1.totalAmount == o2.totalAmount){
                   return 0;
              }else if(o1.totalAmount > o2.totalAmount){
                   return 1;
             }else if (o1.totalAmount < o2.totalAmount){
                   return -1;
              }
    }
});
0
 

Author Comment

by:ip_packet
Comment Utility

Thanks, ;-)

One thing, I neded to add a return statement to the compare method
0
 
LVL 8

Expert Comment

by:mnrz
Comment Utility
>>One thing, I neded to add a return statement to the compare method

is this question? I don't know what you meant by that
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now