?
Solved

Sort a list

Posted on 2007-11-14
5
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:ysnky
ID: 20279485
0
 
LVL 8

Accepted Solution

by:
mnrz earned 1300 total points
ID: 20279493
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
ID: 20280395

Thanks, ;-)

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

Expert Comment

by:mnrz
ID: 20280985
>>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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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