Damini Mehra Damini Mehra - 2 months ago 14
JSON Question

Sort Array list using POJO class with particular status

I have status like "In consultation","Waiting in Queue". all the data coming from json API and add all that data into array list using Pojo class.then how to sort this array list by Consultation status.

Patient Class:

public class Patient_Get_Set implements Serializable {
private String fullName;
private String age;
private String waitTime;
private String sex;
private String patientID;
private String gender;
private String dateOfBirth;
private String phoneNo;
private String Diagnosis;
private String Email;
private String token_id;
private String priority;
private String consultation_status;
private String case_number;

public String getCase_number() {
return case_number;
}

public void setCase_number(String case_number) {
this.case_number = case_number;
}

public String getConsultation_status() {
return consultation_status;
}

public void setConsultation_status(String consultation_status) {
this.consultation_status = consultation_status;
}

public String getPriority() {
return priority;
}

public void setPriority(String priority) {
this.priority = priority;
}

public String getToken_id() {
return token_id;
}

public void setToken_id(String token_id) {
this.token_id = token_id;
}

public String getConsultationstatus() {
return consultationstatus;
}

public void setConsultationstatus(String consultationstatus) {
this.consultationstatus = consultationstatus;
}

private String consultationstatus;

public String getEmail() {
return Email;
}

public void setEmail(String email) {
Email = email;
}

public String getVisitorType() {
return visitorType;
}

public void setVisitorType(String visitorType) {
this.visitorType = visitorType;
}

private String visitorType;


public String getDiagnosis() {
return Diagnosis;
}

public void setDiagnosis(String diagnosis) {
Diagnosis = diagnosis;
}

private String patientJsonArray;

public void setFullName(String fullName) {
this.fullName = fullName;
}

public void setAge(String age) {
this.age = age;
}

public void setWaitTime(String waitTime) {
this.waitTime = waitTime;
}

public String getFullName() {
return fullName;
}

public String getAge() {
return age;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getSex() {
return sex;
}

public String getWaitTime() {
return waitTime;
}


public void setPatientID(String patientID) {
this.patientID = patientID;
}

public void setGender(String gender) {
this.gender = gender;
}

public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public String getDateOfBirth() {
return dateOfBirth;
}

public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}

public String getPhoneNo() {
return phoneNo;
}

public String getPatientID() {
return patientID;
}

public void setPatientJsonArray(String patientJsonArray) {
this.patientJsonArray = patientJsonArray;
}

public String getPatientJsonArray() {
return patientJsonArray;
}


public String getGender() {
return gender;
}
}

Answer

Here is code what you can use for your custom need -

Collections.sort(list, new Comparator<Patient_Get_Set>() {
        @Override
        public int compare(final Patient_Get_Set object1, final Patient_Get_Set object2) {
            List<String> statusList = new ArrayList();
            statusList.add("consultation");statusList.add("queue"); statusList.add("waiting");
            return new Integer(statusList.indexOf(object1.getConsultation_status())).compareTo(new Integer(statusList.indexOf(object2.getConsultation_status())));
            }
        });

add elements in statusList in same order in which order you want pojo class to be sorted.