Хочу отсортировать массив с помощью метода compareTo по имени и фамилии - не выходит, по среднему баллу (AveMark) тоже, хотя НУЖНО по нему (среднему баллу) отсортировать массив. Помогите пожалуйста
Main.java
package com.company;
public class Main {
public static void main(String[] args) {
Student st1 = new Student("Nastia","Polishuk", 1.2, true);
Student st2 = new Student("Pavlo","Kush",1.3, false);
Student st3 = new Student("Ksenia","Lazar", 1.1, true);
Student st4 = new Student("Artem","Germanchuk", 1.4, true);
Student st5 = new Student("Olga","Levan", 2.3, true);
Student st6 = new Student("Artur","Jun", 2.1, false);
Student st7 = new Student("Liza","Osadchuk", 1.9, true);
Student st8 = new Student("Masha","Rubachok", 2.4, true);
Student st9 = new Student("Katya","Pisarchuk", 2.6, true);
Student st10 = new Student("Sasha","Tushenko", 2.5, false);
Student st11 = new Student("Victor","Nazarchuk", 3.1, true);
Student st12 = new Student("Lubov","Po", 3.2, true);
Student st13 = new Student("Asya","Lubid", 3.7, true);
Student st14 = new Student("Nazar","Pulupchuk", 3.4, false);
Student st15 = new Student("Enver","Kozachok", 4.1, true);
Student st16 = new Student("Vova","Desnov", 3.5, true);
Student st17 = new Student("Dima","Korolenko", 4.2, true);
Student st18 = new Student("Diana","Yovko", 4.9, false);
Student st19 = new Student("Marina","Mereshko", 1.7, true);
Student st20 = new Student("Ulyana","Gerasumchuk", 5.0, true);
Student arr[] = new Student[20];
arr[0] = st1;
arr[1] = st2;
arr[2] = st3;
arr[3] = st4;
arr[4] = st5;
arr[5] = st6;
arr[6] = st7;
arr[7] = st8;
arr[8] = st9;
arr[9] = st10;
arr[10] = st11;
arr[11] = st12;
arr[12] = st13;
arr[13] = st14;
arr[14] = st15;
arr[15] = st16;
arr[16] = st17;
arr[17] = st18;
arr[18] = st19;
arr[19] = st20;
System.out.println(" М А С И В Д О С О Р Т У В А Н Н Я: " + "\n");
PrintArr(arr);
System.out.println(" М А С И В П І С Л Я С О Р Т У В А Н Н Я (вставками): " + "\n");
StudentInsetSorting(arr);
PrintArr(arr);
System.out.println(" І Н Д Е К С Ш У К А Н О Г О С Т У Д Е Н Т А ");
System.out.println(" З С Е Р Е Д Н І М Б А Л О М 4.2 : " + "\n");
int index =(int) InterpolationSearch(arr, 4.2);
System.out.println(index);
System.out.println(" М А С И В П І С Л Я В И Д А Л Е Н Н Я: ");
Remove(arr, index);
}
public static void StudentInsetSorting(Student[] arr){// сортування вставками
for(int i = 1; i < arr.length; ++i){
int j = i-1;
Student key = arr[i]; //ми беремо один елемент масиву (спочатку перший, потім другий і т.д.)
while(j >= 0 && key.compareTo(arr[j]) == -1){ //а тоді, порівнюючи з усіма попередніми, знаходимо йому місце
arr[j + 1] = arr[j];
--j; // j = -1 для того щоб далі змінити місцями з елементом
}
arr[j+1] = key; //вставляємо елемент якому знайшли місце
}
}
public static double InterpolationSearch(Student[] arr, double AveMarkToSearch) {
int startIndex = 0;
int lastIndex = arr.length -1;
while((startIndex <= lastIndex) && (AveMarkToSearch >= arr[startIndex].AverageMark()) && AveMarkToSearch <= arr[lastIndex].AverageMark()) {
// используем формулу интерполяции для поиска возможной лучшей позиции для существующего элемента
int pos =(int)( startIndex + (((lastIndex-startIndex) /
((arr[lastIndex].AverageMark() - arr[startIndex].AverageMark()))*(AveMarkToSearch - arr[startIndex].AverageMark()))));
if (arr[pos].AverageMark() == AveMarkToSearch)
return pos;
if (arr[pos].AverageMark() < AveMarkToSearch)
startIndex = (int) (pos + 1);
else
lastIndex = (int) (pos - 1);
}
return -1;
}
public static void Remove(Student [] arr, int index) {
for(int i = 0; i < arr.length; i++) {
if( i == index && arr[i].playOnMusicInstruments() == true) {
Student[] copy = new Student[arr.length-1];
System.arraycopy(arr, 0, copy, 0, i);
System.arraycopy(arr, i+1, copy, i, arr.length-i-1);
PrintArr(copy);
}
}
}
public static void PrintArr(Student[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println("Name: " + arr[i].name() + "," + " surname: " + arr[i].surname() + "," +
" average mark: " + arr[i].AverageMark() + "," + " Play on music instruments: " + arr[i].playOnMusicInstruments());
}
}}
student.java
package com.company;
public class Student implements Comparable <Student> {
private String name;
private String surname;
private double AverageMark;
private boolean playMusic;
public Student(String name, String surname, double averMark, boolean playMusic){
this.name = name;
this.surname = surname;
this.AverageMark = averMark;
this.playMusic = playMusic;
}
@Override
//реализуем метод compareTo интерфейса Comparable
public int compareTo(Student o) {
//используем метод compareTo из класса String для сравнения имен
int result = this.name.compareTo(o.name);
//если имена одинаковые - сравниваем возраст, используя метод compareTo из класса Integer
if (result == 0) {
result = this.surname.compareTo(o.surname);
}
return result;
}
public String name () {
return name;
}
public String surname () {
return surname;
}
public double AverageMark () {
return AverageMark;
}
public boolean playOnMusicInstruments () {
return playMusic;
}
}