使用具有特定状态的POJO类对数组列表进行排序

时间:2017-04-13 08:37:36

标签: java android arrays json arraylist

我的身份类似于"咨询","等待队列"。来自json API的所有数据,并使用Pojo类将所有数据添加到数组列表中。然后如何按咨询状态对此数组列表进行排序。

患者类:

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

3 个答案:

答案 0 :(得分:2)

以下是您可以根据自定义需求使用的代码 -

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())));
            }
        });

按照您希望对pojo类进行排序的顺序,在statusList中添加元素。

答案 1 :(得分:0)

您可以使用Collections对数组列表对象进行排序。

例如您的列表如下。

ArrayList<Patient> list = new ArrayList<Patient>();

然后您可以使用以下代码对列表进行排序。

  Collections.sort(list, new Comparator<Patient>() {
      @Override
      public int compare(final Patient object1, final Patient object2) {
          return object1.getConsultation_status().compareTo(object2.getConsultation_status());
      }
  });

OR

如果您使用的是 Java版本1.8 ,则可以使用以下代码对列表进行排序。

list.stream()
.sorted((object1, object2) -> object1.getConsultation_status().compareTo(object2.getConsultation_status()));

答案 2 :(得分:-2)

import static java.util.Comparator.comparing;

Collections.sort(list,comparison(MyObject :: getStartDate));

将数据与java8中的比​​较进行比较,跟随http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html