0

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;
}
Dharam Dutt Mishra
  • 677
  • 1
  • 6
  • 11
  • To avoid sorting by patient ID, instead of `return t1.first.getPatientId().compareTo(t2.first.getPatientId());` use `return 0;`? – Ole V.V. Dec 18 '18 at 10:18
  • I recommend you avoid the `Date` and `SimpleDateFormat` classes. They are not only long outdated, the latter is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 18 '18 at 19:13
  • can you please send me the implementation of code because i m new in android so its difficult to understand the how to implement. – Dharam Dutt Mishra Dec 20 '18 at 04:53
  • Sorry, it’s hard for me to provide working code. I cannot get your code to work here because it depends on classes and methods that I don’t have. And I’m not sure I have understood your problem. A (reduced) example of desired result and observed result would help so we can see exactly how they differ. Which code works for you also depends on your API level, which I din’t know. – Ole V.V. Dec 20 '18 at 05:17

1 Answers1

0

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161