PostContingencyResultComparator implements Comparator<PostContingencyResult> { @Override public int compare(PostContingencyResult result1, PostContingencyResult result2) { Objects.requireNonNull(result1); Objects.requireNonNull(result2); return result1.getContingency().getId().compareTo(result2.getContingency().getId()); }  @Override int compare(PostContingencyResult result1, PostContingencyResult result2);  }
@Test public void compare() { Contingency contingency1 = Mockito.mock(Contingency.class); Mockito.when(contingency1.getId()).thenReturn("contingency1"); PostContingencyResult result1 = new PostContingencyResult(contingency1, true, Collections.emptyList()); Contingency contingency2 = Mockito.mock(Contingency.class); Mockito.when(contingency2.getId()).thenReturn("contingency2"); PostContingencyResult result2 = new PostContingencyResult(contingency2, true, Collections.emptyList()); Contingency contingency3 = Mockito.mock(Contingency.class); Mockito.when(contingency3.getId()).thenReturn("contingency3"); PostContingencyResult result3 = new PostContingencyResult(contingency3, true, Collections.emptyList()); List<PostContingencyResult> results = Arrays.asList(result3, result1, result2); Collections.sort(results, new PostContingencyResultComparator()); assertEquals(result1, results.get(0)); assertEquals(result2, results.get(1)); assertEquals(result3, results.get(2)); }