I am trying to sort a list of type A named BinOrder in class B according to Class A's int r.
However i am receiving this error for the line Collections.sort(BinOrder);
The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<A>)
Class A:
public class A{
int s;
int r;
public A(int si, int ri) {
s=si;
r= ri;
}
}
Class B:
import java.util.ArrayList;
import java.util.Collections;
public class B implements Comparable<A> {
public Iterator<A> randomMethodName(int a) {
ArrayList<A> BinOrder = new ArrayList<A>();
A a = new A(1,3)
A a2 = new A(1,4)
BinOrder.add(a);
BinOrder.add(a2);
}
// sort array in increasing order of r
Collections.sort(BinOrder);
return BinOrder;
}
@Override
public int compareTo(A list) {
return null;
}
}
To be able to use the single-argument version of
Collection.sort()
on anArrayList
ofA
,A
should implement theComparable
interface:Here's the signature of Collections.sort :
A
must implement Comparable for this method.You try to pass
BinOrder
to this method, whenBinOrder
is of typeArrayList<A>
, but sinceA
does not implementComparable<A>
, it doesn't fit the signature of the method.Either change
A
to implement Comparable, or use thesort
method that accepts a Comparator :