I have an arraylist of custom objects say List<PersonInfo>
PersonInfo list have attributes like
personId
PersonName
PersonAddress.
Suppose the list contains records as {(1,"Name1","Address1"),(3,"Name3","Address3"),(2,"Name2","Address2")}
Now i want to sort the arraylist on the basis of personId in descending order.
that means my output list would be {(3,"Name3","Address3"),(2,"Name2","Address2")(1,"Name1","Address1")}
In addition, if you can, please check out Chapter 7 of the K&B SCJP 6 Study
Guide. It includes some examples that are highly similar to the situation you
describe, and shows how to implement the compareTo() method in detail.
Also, you are right about using the Collections.sort() method.
Abhineet Kapil wrote:There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...
Really? Nothing in the compareTo() method javadoc says anything about only being able to compare on one property. It seems to me that would be a matter of how you implement compareTo().
Really? Nothing in the compareTo() method javadoc says anything about only being able to compare on one property. It seems to me that would be a matter of how you implement compareTo().
There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...
The point is that you can override int compareTo(Object o) method only once in a class that implements Comparable interface...
So you can do it for any one of the property...
Or Please provide an example where we can sort an array with different properties (say empID and empAge)..
Abhineet Kapil wrote:The point is that you can override int compareTo(Object o) method only once in a class that implements Comparable interface...
So you can do it for any one of the property...
It doesn't matter that the compareTo method is called only once.
To sort on multiple properties, do something like this in your compareTo method:
1. Compare the primary properties you want to sort on.
2. If they are not equal, then return the appropriate value (-1 or 1).
3. If they are equal, then compare the secondary properties you want to sort on.
4. If they are not equal, then return the appropriate value (-1 or 1).
5. If they are equal, then compare the ternary properties you want to sort on.
...
6. If all properties are equal, return 0.
It doesn't matter that the compareTo method is called only once.
To sort on multiple properties, do something like this in your compareTo method:
1. Compare the primary properties you want to sort on.
2. If they are not equal, then return the appropriate value (-1 or 1).
3. If they are equal, then compare the secondary properties you want to sort on.
4. If they are not equal, then return the appropriate value (-1 or 1).
5. If they are equal, then compare the ternary properties you want to sort on.
...
6. If all properties are equal, return 0.
Jesper..
You are going a little off the track..
Consider following table..
Now lets sort it by EMP NAme..
(Here we are putting secondary check on EMP AGE so Santosh comes before Sahil)
Eventhough we are putting a secondary check on age , that doesnt mean we are sorting empArray by age...
As you can clearly see age column is still unsorted.. In reality we are still sorting by EmpName..
So for one overrided compareTO() method we can sort by one property only.
That is incorrect: we're talking about sorting by name *and* age. Your sample size is too small to see what this actually means in practice. If two of those rows had the same name (or the same age), they'd be sorted on the second criteria (age or name, depending on what order you're sorting in). If you were sorting by name then age, and two users had the name "Santosh", they'd then be sorted by age. This happens in SQL criteria all the time, in pseudo-SQL:A compareTo method can do the exact same thing.
Sorting on multiple criteria means you sort by one *then* another--one takes precedence.
I take your point. But what I am talking about is a completely different thing.
One thing has to be made very clear that a collection can be sorted by only one property at a time.
Let me take an example to make it clear.
Requirement : Display two different results.
1. empArray sorted by empID.
2. empArray sorted by empName.
Assumption : Employee bean class implements Comparable interface and overrides compareTO() for comparing empID..
My Argument :
compareTO() method can be overridden only once.
It compares empID (If empID is equal then compares by empName).
Following above procedure will display one set of data that will be sorted by empID.
Then you probably meant to say "you can only implement a single sort order using Comparable". (Which is also true only if your compareTo doesn't do any logic to decide how to sort.)
David Newton
Today 15:23:40 Subject: Sorting arraylist on the basis of object
Then you probably meant to say "you can only implement a single sort order using Comparable". (Which is also true only if your compareTo doesn't do any logic to decide how to sort.)
If compareTo() doesnt provide any sorting logic for a custom object (other than primitives),you will get a runtime error when you try Collections.sort(customObject).
I'm not sure what that has to do with what I said, but okay. I meant that compareTo could decide on what property, or properties, to sort, based, say, on other instance variables.
Post by:autobot
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, the permaculture playing cards