У меня проблема с частью моего кода.Он должен сортировать по имени в алфавитном порядке, но, похоже, делает это по убыванию.Может ли кто-нибудь подсказать мне, какие изменения я должен внести, чтобы выходные данные правильно отображали данные?Спасибо.
Вот мой код сортировки:
//Method sortName
public static void sortName() throws IOException {
//Selection Sort
for (x = 0; x < 8; x++) {
smallest = x;
for(i = x + 1; i < 8; i++) {
//Compare current smallest
//to the current position in the array
if(name[i].compareTo(name[smallest])> 0) {
smallest = i;
}
}
//Swap smallest element with position in array
temp = name [x];
name [x] = name [smallest];
name [smallest] = temp;
temp = crime [x];
crime [x] = crime [smallest];
crime [smallest] = temp;
temp = year [x];
year [x] = year [smallest];
year [smallest] = temp;
}
//Display each category of records; names, crime, year
System.out.print("Name" + " ----" + "Crime" + "----" + "Year\n");
//output
for (x = 0; x < 8; x++) {
//Display each sorted criminal name with the crime and year.
System.out.println(name[x] + " --- " + crime[x] + " --- " + year[x]);
Вот мой вывод:
Name ----Crime----Year
Slippery Sal --- Arson --- 1997
Natasha Ora --- Theft --- 2007
Kate Olaf --- Assault --- 1984
Eddie Striker --- Arson --- 1978
Bugs Malone --- Theft --- 1981
Bob Allen --- Assault --- 1957
Anne Wilson --- Arson --- 2013