Salve,
stavo provando a ordinare un array utilizzando l' interfaccia Comparator ma non riesco a capire perchè mi da un ordinamento sbagliato.
Il codice è questo, qualcuno può aiutarmi a capire dove sbaglio?
codice:
import java.util.*;
class CompareStudent implements Comparator{
public int compare (Object s1, Object s2){
return ((Student)s1).getname().compareTo(((Student)s1).getname());
}
}
public class Student {
String nome;
String cognome;
String matricola;
Student(String nome, String cognome, String matricola){
this.nome=nome;
this.cognome=cognome;
this.matricola= matricola;
}
String getname(){
return this.nome;
}
String getsurname(){
return this.cognome;
}
public static void main(String[] args){
Student[] listastudenti= new Student[5];
listastudenti[1]= new Student("Luca","Boh","66692");
listastudenti[0]= new Student("Dario","Rossi","66837");
listastudenti[4]= new Student("Paolo","Bianchi","65562");
listastudenti[3]= new Student("Alex","Rfk","48912");
listastudenti[2]= new Student("Sara","Blu","65522");
Arrays.sort(listastudenti, new CompareStudent());
for (int i=0; i<listastudenti.length;i++){
System.out.println((listastudenti[i].getname()) +" " + (listastudenti[i].getsurname()) );
}
}
}