下面是我编写的代码,用于将所有字谜放在一组字符串中。输出没有按预期排序。实际上,输出与输入相同。我哪里错了?
package set2;
import java.util.Arrays;
import java.util.Comparator;
public class printAllAnagrams {
public static void main(String[] args) {
String[] s = { "Harsha", "ant", "sha", "tna", "ash" };
sortAnagrams(s);
for (String e : s) {
System.out.println(e);
}
}
private static void sortAnagrams(String[] s) {
Arrays.sort(s, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
s1.toLowerCase();
s2.toLowerCase();
if (s1.length() != s2.length()) {
return -1;
} else {
char[] s1_char = s1.toCharArray();
char[] s2_char = s2.toCharArray();
Arrays.sort(s1_char);
Arrays.sort(s2_char);
for (int i = 0; i < s1_char.length; i++) {
if (s1_char[i] != s2_char[i]) {
return -1;
}
}
}
return 0;
}
});
}
}