-5

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.

1 Answers1

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