Solved

Sort a list

Posted on 2007-11-14
5
211 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 126
split string containing \r\n in Java 46 38
java stored proc example 9 19
arguments to jar 5 10
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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

914 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

15 Experts available now in Live!

Get 1:1 Help Now