Sorting a List of Items

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 22
Reputation: a_iyer20 is an unknown quantity at this point 
Solved Threads: 0
a_iyer20 a_iyer20 is offline Offline
Newbie Poster

Sorting a List of Items

 
0
  #1
Apr 11th, 2008
Hello,

After having a list created from objects of a certain Class. I wish to sort the contents. How do I do this?

This is my case:

_bkgList = new ArrayList();
for(int i = 0; i<10; i++){
BookingResponse bkngRes = new BookingResponse();
bkngRes.setAwbNum(awbNum);
bkngRes.setOrigin(origin);
_bkgList.add(bkngRes);
}

As you can see, I have created the list with items as they get added. Now, I wish to display this list in a manner such that the items are displayed with (for eg) the Origin as key sorted in a alphabetical order. How can I get this implemented. Please suggest.

Thanks,
Ashok.
Last edited by a_iyer20; Apr 11th, 2008 at 5:01 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Sorting a List of Items

 
0
  #2
Apr 11th, 2008
I believe that the Collections class has a sort method: Here is what you must do:

Your object that you put inside the ArrayList has to implement the Comparable interface. When you do that you will have to implement the compareTo method that compares this object with another object. This method returns a positive number if this object is greater, 0 if it is equal and negative if it is lower.
Check the API for the Comparable interface and the compareTo method.

Then use the sort method of the Collections class that is static and takes as argument your ArrayList.
Check the API for the classes: java.util.ArrayList , java.util.Collections, java.lang.Comparable
If you don't know the links for the API's let me know
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Sorting a List of Items

 
0
  #3
Apr 11th, 2008
One thing I forgot. The compareTo method will compare the objects in any way you like. Meaning that in your case you will only use the Origin in order to compare the objects. If it is a String use the compareTo method of the String class as your return statement.
And as you will read if you return 0, the equals method doesn't have to return true.

Meaning that you might want to sort two object based on the 'age' for example but the equals method will use the 'name' in order to determine if they are equal. That is for a hypothetical Person object
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: a_iyer20 is an unknown quantity at this point 
Solved Threads: 0
a_iyer20 a_iyer20 is offline Offline
Newbie Poster

Re: Sorting a List of Items

 
0
  #4
Apr 11th, 2008
Yes from your suggestion and also from some other internet materials, I got to learn that Comparator interface is the start. So i did the following:
1. public class BookingResponse implements Comparable{

2. the compareTo() got autoInserted into the same class.

3. To sort the list, I used Collections.sort(_bkgList);

I did not know what else should I do with the origin. Could you now give me a small snippet explaining what I am missing.

Thanks,
Ashok.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Sorting a List of Items

 
1
  #5
Apr 11th, 2008
Inside the compareTo method you will say how you want the objects to be compared.
Assume you have an Object with attributes name, height and weight.

If you want them to be sorted based on the height. write: (I don't remember the exact signature of compareTo so just pay attention to what is inside)
  1. int compareTo(AnObject obj) {
  2. if (this.height>obj.height) return 1; //a positive number
  3. if (this.height<obj.height) return -1; //a negative number
  4. return 0; //if both heights are the same
  5. }
Do the same if you want them to be sorted based on their weight.
Remember that equals is used for comparing if two objects have the same values so when the compareTo returns 0 doesn't necessary means that equals will return true. It may do, it do not.

About your case based on what you want them to be sorted? if it is based on Origin as you said, and if Origin is String then:
  1. int compareTo(BookingResponse obj) {
  2. return Origin.compareTo(obj.Origin);
  3. }
Or you can use your own criteria. Just remember to return positive, negative or 0 depending on how you want them to be compared
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: a_iyer20 is an unknown quantity at this point 
Solved Threads: 0
a_iyer20 a_iyer20 is offline Offline
Newbie Poster

Re: Sorting a List of Items

 
0
  #6
Apr 11th, 2008
Thanks, you guided me to the right area. however, an example from the link below was exactly the same as what I required.

http://www.java-tips.org/blog/java-s...rt-method.html

Thanks a lot!
Ashok.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: a_iyer20 is an unknown quantity at this point 
Solved Threads: 0
a_iyer20 a_iyer20 is offline Offline
Newbie Poster

Re: Sorting a List of Items

 
0
  #7
Apr 11th, 2008
How can I close this thread and mark it as solved? I am relatively new to DaniWeb.

Oh oh ....just before I call it close, one thing, I have managed to get my list sorted out using one paramater. But the list also needs to e sorted out another criterion.

In my case, origin as you know already and awbNum as I have mentioned initially. Could you please advice.

Thanks,
Ashok.
Last edited by a_iyer20; Apr 11th, 2008 at 7:14 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Sorting a List of Items

 
0
  #8
Apr 11th, 2008
Somewhere in this page there has to be a link that says Solved
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Sorting a List of Items

 
0
  #9
Apr 11th, 2008
I think I found it. Under your first post there is a link "Reply to Thread" with orange background and an arrow. Next to it, there should be what you are looking for. It is above the area where you can post a quick reply
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: a_iyer20 is an unknown quantity at this point 
Solved Threads: 0
a_iyer20 a_iyer20 is offline Offline
Newbie Poster

Re: Sorting a List of Items

 
0
  #10
Apr 11th, 2008
Yes i found it. One more thing, I need sorting for more than one parameter. My sample link that I showed you shows an eg for one paramete. Could you please guide me for using more than one parameter.

Thanks,
Ashok.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 1525 | Replies: 13
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC