My List is being sorted by the alphabetical order after sorting by date while I'm adding the 4-5 items in my List

廉价感情. 提交于 2021-02-11 15:14:33

问题


My List is being sorted by the alphabetical order after sorting by date while I am adding extra 4-5 items in my List with different names. I want to sort my List by date and time only.

here I have shared my code please help me to do so. thanks in advance.

// Filters All patient to keep only single latest record of each patient id
private List<PatientData> filterPatients(List<PatientData> allPatientData) throws ParseException {

    HashMap<String ,Pair<PatientData,Date>> filtered = new HashMap<>();
    SimpleDateFormat inSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    for(PatientData p : allPatientData){
        if(filtered.containsKey(p.getPatientId())){
            Pair<PatientData,Date> _p = filtered.get(p.getPatientId());
            Date d1 = _p.second;
            Date d2 = inSdf.parse(p.getScanDate());
            if(d2.after(d1)){
                filtered.put(p.getPatientId(), new Pair<>(p, d2));
            }
        }else{
            if(p.getScanDate() != null)
                filtered.put(p.getPatientId(), new Pair<>(p, inSdf.parse(p.getScanDate())));
        }
    }


    List<Pair<PatientData,Date>> filteredPairs = new ArrayList<>(filtered.values());
    Collections.sort(filteredPairs,new Comparator<Pair<PatientData, Date>>() {
        @Override
        public int compare(Pair<PatientData, Date> t1, Pair<PatientData, Date> t2) {
            if(t1.second.after(t2.second)){
                return -1;
            }else if (t1.second.before(t2.second)){
                return 1;
            }
            else
                return t1.first.getPatientId().compareTo(t2.first.getPatientId());
        }
    });
    List<PatientData> filteredList = new ArrayList<>(filteredPairs.size());



    for(Pair<PatientData,Date> p : filteredPairs){
        filteredList.add(p.first);
    }
    return filteredList;
}

回答1:


java.time

First, in your PatientData class don’t use a string for scan date. Use a date-time type and prefer the modern Instant over the old-fashioned Date. As a convenience you may have a constructor and/or a setter that accepts a string and parses it. For example:

public class PatientData {

    private String patientId;
    private Instant scanDate;

    public PatientData(String patientId, String scanDateString) {
        this.patientId = patientId;
        setScanDate(scanDateString);
    }

    public void setScanDate(String scanDateString) {
        scanDate = Instant.parse(scanDateString);
    }

    // getters, etc.
}

Now you have no need for building pairs, you can just use the PatientData class alone. To sort a list of PatientData:

    Collections.sort(filteredList, new Comparator<PatientData>() {
        @Override
        public int compare(PatientData pd1, PatientData pd2) {
            return pd1.getScanDate().compareTo(pd2.getScanDate());
        }
    });

Or if you API level is high enough (corresponding to Java 8):

    Collections.sort(filteredList, Comparator.comparing(PatientData::getScanDate));

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.


来源:https://stackoverflow.com/questions/53827809/my-list-is-being-sorted-by-the-alphabetical-order-after-sorting-by-date-while-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!