Ok, so I have a program with a part that I need to "Order the words such that the last letter of each item in the list is the first letter of the next item, a sort of chain of words linked together by last and first letters."
The sample input is dog,elephant,giraffe,rhinoceros,tiger and the correct output is dog,giraffe,elephant,tiger,rhinoceros while my output is tiger, rhinoceros, dog, giraffe, elephant.
The comparator is this:
class linkedSort implements Comparator {
//will return 1 for a match
//returns 0 if no match
public int compare(Object t, Object t1) {
char[] charArr1 = t.toString().toCharArray();
char[] charArr2 = t1.toString().toCharArray();
if (charArr1[charArr1.length - 1] == charArr2[0]) {
return -1;
} else {
return 1;
}
}
}
Any help would be much appriciated!!