| | |
Sorting a List of Items
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
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
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
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
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
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
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)
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:
Or you can use your own criteria. Just remember to return positive, negative or 0 depending on how you want them 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)
Java Syntax (Toggle Plain Text)
int compareTo(AnObject obj) { if (this.height>obj.height) return 1; //a positive number if (this.height<obj.height) return -1; //a negative number return 0; //if both heights are the same }
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:
Java Syntax (Toggle Plain Text)
int compareTo(BookingResponse obj) { return Origin.compareTo(obj.Origin); }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
http://www.java-tips.org/blog/java-s...rt-method.html
Thanks a lot!
Ashok.
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
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
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Sockets
- Next Thread: Validate jTable cell value.
Views: 1525 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for Java
addball android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time transfer tree trolltech utility windows






