Sorting string lengths using a comparator

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>(); //add contents to arraycat String[] array = arraycat.toArray(new String[0]); //array looks like this now: //array=[cat,cataaaa,cataa,cata,cataaa] 

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.

+7
source share
4 answers

You need to specify the type parameter for Comparator for your work.

 class comp implements Comparator<String> { 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; } } } 

In Java 1.7 and later, you can also simplify the body of this method:

 class comp implements Comparator<String> { public int compare(String o1, String o2) { return Integer.compare(o1.length(), o2.length()); } } 

In addition, Collections.sort sorts List objects. Since you are sorting an array, you should use Arrays.sort :

 Arrays.sort(array, new comp()); 
+13
source

You need to use the Arrays.sort() method if the data source is array .

For example,

 String []array={"first","second","third","six"}; Arrays.sort(array,new Comparator<String>() { public int compare(String s1,String s2) { return s1.length() - s2.length(); } }); 

Or convert the array to a list to use the Collections.sort () method,

 Collections.sort(Arrays.asList(array),new Comparator<String>() { public int compare(String s1,String s2) { return s1.length() - s2.length(); } }); 
+6
source

Must be

 class comp implements Comparator<String> { ... 

or even better

 Collections.sort(array, new Comparator<String> { ... 

(and don't even name the class that is used only once)

+2
source

I recently had a similar task, and I will give you an additional example.

 import java.util.Arrays; import java.util.Comparator; //for more information: http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html /** * * @author Xilef */ public class StringKorter implements Comparator<String> { @Override public int compare(String s1, String s2){ if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else return 0; } public static void main(String[] args) { String[] woorden = { "boot", "kinderen", "stoel", "volwassenen", "ei", "stoel", "kop", "zeshoek", "stoel", "ei" }; System.out.println("woorden: " + Arrays.toString(woorden));//before sorting by length Arrays.sort(woorden, new StringKorter()); System.out.println("Array woorden after sorting by length: " + Arrays.toString(woorden)); } } 
0
source

All Articles