4

I'm having a hard time sorting out my objects according to its properties.

I have this list of patient objects that I want to sort according to their last name but it doesn't sort them at all. Can you tell me what am I doing wrong?

Here are some relevant code.

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;
        }
}
}
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
philip
  • 1,292
  • 3
  • 24
  • 44
  • @VikalpPatel shouldn't the sort using a comparator as a second parameter work according to the comparator only? That's my understanding from http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator) – Erik Madsen Jun 05 '14 at 09:33
  • @ErikMadsen : Went through documention, sorry for the misleading about order of comparable and comparator which doesn't exist at all. Totally agreed with you. – Vikalp Patel Jun 05 '14 at 09:39
  • 1
    @ErikMadsen You are correct. I executed the code and printed the patient names before and after sorting and it is working fine. – todd Jun 05 '14 at 10:14
  • @todd How were you able to make it work? can you show me your source code? – philip Jun 06 '14 at 00:36

2 Answers2

1

As far as I can see your code should work, and I suspect a unit test would prove it. If your problem is that you cannot see the update, maybe your view updating logic is at fault.

Erik Madsen
  • 1,913
  • 3
  • 20
  • 34
  • Is that so. What do you suggest I do in order to correct this? Is it possible to pass the updates to a new object? – philip Jun 06 '14 at 00:34
  • I was finally able to make it work. It seems that the problem was I am an idiot. I displayed the initial list and before sorting it. That's why the new sorted list aren't showing correctly. Thanks for the help. – philip Jun 06 '14 at 01:23
1

Philip, I just copied your code and pasted it in a java project. The source code is below along with a screen shot of the output. I didn't make any changes to the Patient class; just copied and pasted it into my project.

Test.java:

import java.util.ArrayList;
import java.util.Collections;

public class Test {

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

    public static void main(String[] args) {

        createPatientList();

        System.out.println("Before: ");
        for (Patient p : patientList) {
            System.out.println(p.lname);
        }

        sortPatient(1);

        System.out.println("\nAfter: ");
        for (Patient p : patientList) {
            System.out.println(p.lname);
        }
    }

    private static 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 static 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;
        }
    }
}

Screen shot:

screen shot

todd
  • 1,266
  • 1
  • 13
  • 20
  • I fixed the problem already just check my comment to @Erik answer. Thanks for the help anyway. :) – philip Jun 06 '14 at 01:39