For example, if I have an arraylist contains one kind of Object that has a date field implement comparable interface, like a string. How can I sort this arraylist using this date field. It is a generic method, therefore I can't write my own comparator method.
Asked
Active
Viewed 104 times
-5
-
See the second answer in linked post. – Gaurava Agarwal Jul 12 '16 at 05:51
-
2@GauravaAgarwal I think I should give more details about my question. It is a generic method, therefore I can't write my own comparator method – Stewie Griffin Jul 12 '16 at 05:55
-
Can you post fields inside your target class, whose object you wish to sort? – Gaurava Agarwal Jul 12 '16 at 06:00
-
@GauravaAgarwal Generic Type T,that might be any object that has a unknown type comparable field R – Stewie Griffin Jul 12 '16 at 06:10
1 Answers
0
Your problem is not very clear to me. But based on my understanding I have written some code. Kindly give your feedback if below solution is somewhat near to your problem.
interface DateCompare<H extends DateCompare> extends Comparable<H> {
public Date getDate();
}
class ActualClass<H extends DateCompare> implements DateCompare<H> {
Date date;
public ActualClass() {
date=new Date();
}
public int compareTo(H o) {
return this.getDate().compareTo(o.getDate());
}
public Date getDate() {
return date;
}
}
class ActualService <T extends DateCompare<DateCompare>> {
List<T> list = new ArrayList<T>();
}

Gaurava Agarwal
- 974
- 1
- 9
- 32