java – 可比较不排序对象

我很难根据它的属性整理我的对象.

我有这个患者对象列表,我想根据他们的姓氏进行排序,但它根本不对它们进行排序.你能告诉我我做错了什么吗?

这是一些相关的代码.

Patient.java

import java.util.Comparator;

public class Patient implements Comparable<Patient> {
    String fname, lname, mname, ID;

    public static class OrderByLastName implements Comparator<Patient> {

        @Override
        public int compare(Patient p1, Patient p2) {
            return p1.lname.compareTo(p2.lname);
        }
    }

    public static class OrderByID implements Comparator<Patient> {

        @Override
        public int compare(Patient p1, Patient p2) {
            return p1.ID.compareTo(p2.ID);
        }
    }

    @Override
    public int compareTo(Patient another) {
        // TODO Auto-generated method stub
        return 0;
    }
}

MainActivity.java

public class PatientList extends ActionBarActivity {

String[] newFName = {"Mark","Andy","Bryan"};
String[] newLName = {"Uy","Igy","Nator"};
String[] newMName = {"Wi","Menos","Pat"};
String[] newID = {"3","5","1"};
ArrayList<Patient> patientList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createPatientList();
    sortPatient(1);

}

private void createPatientList() {
    patientList = new ArrayList<Patient>();
    for (int i = 0; i < newFName.length; i++) {
        Patient patient = new Patient();
        patient.lname = newLName[i];
        patient.fname = newFName[i];
        patient.mname = newMName[i];
        patient.ID = newID[i];
        patientList.add(patient);
    }
}

private void sortPatient(int order){
    switch (order) {
        case 1:
            Collections.sort(patientList, new Patient.OrderByLastName());
            break;
        case 2:
            Collections.sort(patientList, new Patient.OrderByID());
            break;
        default:
            break;
        }
}
}

解决方法:

据我所知,你的代码应该可行,我怀疑单元测试会证明这一点.如果您的问题是您无法看到更新,那么您的视图更新逻辑可能有问题.

上一篇:集合的复杂排序


下一篇:java – 继承Compable与泛型