所以我试图对包含我的Student类的对象的LinkedList进行排序,其中包含名称id#作为字符串,以及gpa double。我想按名字排序。
public class Student implements Comparable<Student>, Serializable {
private String name;
private String gNumber;
private double gpa;
public Student(String name, String gNumber, double gpa){
this.name = name;
this.gNumber = gNumber;
this.gpa = gpa;
}
public int compareTo(Student s) {
return this.name.toLowerCase().compareTo
(s.getName().toLowerCase());
}
这是我的LinkedList类和尝试排序的方法
public class LinkedList extends AbstractList implements Serializable{
//some code
public void Sort(){
Collections.sort(this, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
}
然而,一旦我真正尝试对它进行排序,就没有任何变化,我没有任何错误或任何事情它只是没有发生任何事情。我使用我的调试器跟踪该过程,它正确调用方法并进入方法代码,但我的LinkedList实际上没有发生任何事情。任何想法?
编辑:这是我的设置(插入)方法:
public void insert(Student student){
//case where input is null
if(student.getName() == null || student.getGNumber() == null)
throw new IllegalArgumentException();
Node temp = head;
//case where there is no list
if(head == null){
head = new Node(student, null);
tail = head;
count = 1;
}
//case where top has existing Nodes
else{
//brings reference to end of list
while(temp.getNext() != null)
temp = temp.getNext();
temp.setNext(new Node(student, null));
tail = temp;
count ++;
}
}