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.

Recommended Answers

All 13 Replies

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

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

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.

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)

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
}

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:

int compareTo(BookingResponse obj) {
   return Origin.compareTo(obj.Origin);
}

Or you can use your own criteria. Just remember to return positive, negative or 0 depending on how you want them to be compared

commented: Clear and helpful +6

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.

Somewhere in this page there has to be a link that says Solved

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

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.

First of all you can have only one compareTo method, meaning that you can only have one rule for sorting. You cannot dynamically choose based on what you will sort. That is defined in the compareTo method. When you implement the method it will only sort based on that. If you want to sort using something else you will have to change the method.
The sort method of Collections will use a specific algorithm for sorting. As you know any sorting algorithm compares the elements it will sort with each other in order to see which is greater or lower in order to sort them. But how will the sort method of Collections determine which is greater or lower in order to sort them? It will use compareTo method and you will have to tell it when it compares two elements which one is greater or lower in order to do a swap or whatever the algorithm does.
So, when you have two objects, and someone (like Collections.sort()) asks you: Which one is greater?, what will you tell him? I will tell him for eg. that the first is greater because the variable height is greater (or lower, do whatever you want) than the other's objects.
So how will you compare these two BookingResponse objects. What will be the answer to the above question? If you want them to be sorted based on the Origin, then decide which object's Origin is greater, lower or equal than the other and return 1, -1, or 0. Remeber, String already has a compareTo method to be used.

Oh yes, I perfectly agree and understand what you explain. But let me elaborate a little more to what my user demands.

The user wishes to see a table of data with some columns. The 2 major columns out the many are: 1. origin 2. awbNum.

The list of items that populate this data table needs to presented in a manner in which appears sorted 1. by the origin alphabetically and 2. awbNum alphabetically.

for eg:
origin awbNum
BBB cccc
AAA bbbb
CCC aaaa

needs to be listed as:
origin awbNum
AAA aaaa
BBB bbbb
CCC cccc

I have managed to get the list sorted out using one column. If 2 column sorting gets solved, I will be on top of my application and will sleep well. Please advice.

One more thing. This may help in the final solution.
Please go through the link once again:

http://www.java-tips.org/blog/java-se/difference-between-comparable-and-comparator-interface.html

The link also explains the difference between comparator and comparable. compareTo may be useful only to sort based on 1 criterion. however, the compare() method is described to sort based on more than 1 parameter. The example shows how. But it is difficult for me to understand. Please help.

Thanks,
Ashok.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.