Solved

Sort a list

Posted on 2007-11-14
5
220 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
ID: 20279452
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20279485
0
 
LVL 8

Accepted Solution

by:
mnrz earned 325 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 107
ArrayIndexOutOfBoundException 9 84
egit plugin on eclipse 8 65
Convert from a json string array to a Java object 3 35
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

831 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