### Question:
Encounters extends ArrayList<EncounterWithObservations> { public void sortByDate() { Collections.sort(this, new Comparator<EncounterWithObservations>() { @Override public int compare(EncounterWithObservations lhs, EncounterWithObservations rhs) { if (lhs.getEncounter().getEncounterDatetime()==null) return -1; if (rhs.getEncounter().getEncounterDatetime()==null) return 1; return -(lhs.getEncounter().getEncounterDatetime() .compareTo(rhs.getEncounter().getEncounterDatetime())); } }); } Encounters();  Encounters(Observation... observations);  Encounters(List<Observation> observationsByPatient); void sortByDate();  }### Answer:
@Test public void shouldSortTheEncountersByDate() { final Observation observation1 = createObservation(createEncounter("c1", new Date(1)), "01"); final Observation observation2 = createObservation(createEncounter("c2", new Date(3)), "02"); final Encounters encounters = new Encounters(observation1, observation2); encounters.sortByDate(); final Encounters expectedOrderedConcept = new Encounters() {{ add(encounterWithObservations(observation2)); add(encounterWithObservations(observation1)); }}; assertThat(encounters, is(expectedOrderedConcept)); }
@Test public void shouldPutEncounterWithNullDateAtTheTopWhenItsNotAtTheTop() { final Observation observation1 = createObservation(createEncounter("c1", new Date(1)), "01"); final Observation observation2 = createObservation(createEncounter("c2", null), "02"); final Encounters encounters = new Encounters(observation1, observation2); encounters.sortByDate(); final Encounters expectedOrderedConcept = new Encounters() {{ add(encounterWithObservations(observation2)); add(encounterWithObservations(observation1)); }}; assertThat(encounters, is(expectedOrderedConcept)); }
@Test public void shouldPutEncounterWithNullDateAtTheTopWhenItsAtTheTop() { final Observation observation1 = createObservation(createEncounter("c1", null), "01"); final Observation observation2 = createObservation(createEncounter("c2", new Date(1)), "02"); final Encounters encounters = new Encounters(observation1, observation2); encounters.sortByDate(); final Encounters expectedOrderedConcept = new Encounters() {{ add(encounterWithObservations(observation1)); add(encounterWithObservations(observation2)); }}; assertThat(encounters, is(expectedOrderedConcept)); }