I'm learning java and trying out UVA questions. This is UVA 612. I've used the Comparator to sort my String array but i have no idea why the value 9 is larger than all the number. This is my code:
public static String getSort(String str) {
int SLEN = str.length();
int count = 0;
for (int i = 0; i < SLEN; i++) {
for (int j = i; j < SLEN; j++) {
if (str.charAt(i) > str.charAt(j)) {
count++;
}
}
}
return Integer.toString(count);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int SLEN = sc.nextInt();
int LINE = sc.nextInt();
String[][] table = new String[LINE][2];
for (int i = 0; i < LINE; i++) {
table[i][1] = sc.next();
table[i][0] = getSort(table[i][1]);
}
Arrays.sort(table, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String str1 = entry1[0];
final String str2 = entry2[0];
return str1.compareTo(str2);
}
});
for (int i = 0; i < LINE; i++) {
System.out.println(table[i][1]);
}
sc.close();
}
The input is:
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Output I got is:
AACATGAAGG #10
GATCAGATTT #11
ATCGATGCAT #17
TTTTGGCCAA #36
TTTGGCCAAA #37
CCCGGGGGGA #9
But the expected output is:
CCCGGGGGGA #9
AACATGAAGG #10
GATCAGATTT #11
ATCGATGCAT #17
TTTTGGCCAA #36
TTTGGCCAAA #37
The integer behind hash is the value of getSort(). I have no idea why the value 9 is the largest after sorting.