When I try to sort an array based on its element string length, I am hit with a compilation error. I have a set to start,
Set<String> arraycat = new HashSet<String>();
I would ideally like to sort by
array=[cat,cata,cataa,cataaa,cataaaa]
so i have a type comparator
class comp implements Comparator { public int compare(String o1, String o2) { if (o1.length() > o2.length()) { return 1; } else if (o1.length() < o2.length()) { return -1; } else { return 0; } } }
and then I call the class
Collections.sort(array, new comp());
but then it gives me two compilation errors:
comp is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator class comp implements Comparator { ^ testa.java:59: cannot find symbol symbol : method sort(java.lang.String[],comp) location: class java.util.Collections Collections.sort(array, new comp()); ^2 errors
I would appreciate any tips to solve the problem.
Johnj
source share