I want sort an array of Strings using Comparator<T> ( I'm well aware of lambdas in Java 8 ) just wanted to test Comparator<T> for exercise and here is my code :
package ehsan;
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
String[] names = {"Winter","Autumn","Summer","Spring"};
Arrays.sort(names, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1.length()>o2.length())
retu 1;
else if(o1.length()==o2.length())
retu 0;
else
retu -1;
}
});
for (String elem:names) {
System.out.println(elem);
}
}
}
But the result I get ain't what I expect :
Winter
Autumn
Summer
Spring
And it seems it doesn't change anything! Well, I guess I am missing something.
Any help would be appreciated :)
