Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sort a list

Posted on 2007-11-14
5
Medium Priority
?
252 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

609 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