I have an interface, MyInterface
, that extends Comparable since I want to be able to sort lists of these objects:
1 global interface MyInterface
2 extends Comparable {
All classes that implement MyInterface
contain this code to implement the compareTo
method:
(Unfortunately these classes are extending different superclasses, and multiple inheritance isn't possible with Apex)
1 global class SubClass1
2 extends SuperClass1
3 implements MyInterface
4 {
127 public Integer compareTo( Object OtherRecord ){
128 MyInterface OtherMyInterface = (MyInterface) OtherRecord ;
129 if ( this.getTimestamp() > OtherMyInterface.getTimestamp() ) return 1;
130 if ( this.getTimestamp() < OtherMyInterface.getTimestamp() ) return -1;
131 else return 0 ;
132 }
A Visualforce controller is trying to sort the these MyInterface objects, but it is failing with a run-time exception:
System.ListException: One or more of the items in this list is not Comparable
61 /*
62 for (MyInterface s : MyList){
63 System.debug( s instanceOf Comparable );
64 }
65 */
66 MyList.sort();
Attempts to uncomment lines 61-64 fail with a compile time exception:
Operation instanceof is always true since an instance of MyInterface is always an instance of System.Comparable
Does anyone out there have any ideas on what I'm missing here? How is it that one of the objects is not Comparable if all of them are instances of Comparable?
DateTime getTimeStamp();
. Each class has it's own implementation, but that isn't causing any trouble. I've gotten the code work at least, and it looks like theComparable
interface needs to also be explicitly listed in each class that implementsMyInterface
. It seems like the compiler thinks that what I have here is sufficient, but the runtime is having trouble finding what it needs.