This is a method I was supposed to write. It takes two Strings and is supposed to return true if they are anagrams. Also the sorting is demanded.
I'm not allowed to use imports, just used one to see if it sorts correctly. However I don't understand why it still returns false? I read somewhere als that (ArrayA == ArrayB) works as comparison?
public static boolean anagramCheck (String a, String b) {
String[] conA = a.toLowerCase().split("");
String[] conB = b.toLowerCase().split("");
//sort A
for (int i = 0; i < (conA.length-1); i++) {
for (int j = i + 1; j < conA.length; j++) {
if (conA[i].compareTo(conA[j]) > 0) {
String temp = conA[i];
conA[i] = conA[j];
conA[j] = temp;
}
}
}
//sort B
for (int i = 0; i < conB.length; i++) {
for (int j = i + 1; j < conB.length; j++) {
if (conB[i].compareTo(conB[j]) > 0) {
String temp = conB[i];
conB[i] = conB[j];
conB[j] = temp;
}
}
}
System.out.println(Arrays.toString(conA));
System.out.println(Arrays.toString(conB));
boolean result;
if (conA == conB) {
result = true;
return result;
} else {
result = false;
return result;
}
}