@Test public void testWithNullsLast() { Long[] a = new Long[] {null, 5L, 1L, 3L, null, 2L, 4L, null}; Arrays.sort(a, Comparators.withNullsLast(new AscendingLongComparator())); assertArrayEquals(new Long[] {1L, 2L, 3L, 4L, 5L, null, null, null}, a); }
@Test public void testNaturalOrder() { Comparator<Long> natural = Comparators.naturalOrder(); Long[] a = new Long[] {5L, 1L, 3L, 2L, 4L, 1L}; Arrays.sort(a, natural); assertArrayEquals(new Long[] {1L, 1L, 2L, 3L, 4L, 5L}, a); }
@Test public void testCompose() { Comparator<String> nullsLast = new NullsLastStringComparator(); Comparator<String> alphabetical = new AlphabeticalStringComparator(); String[] a = new String[] {null, "b", "d", null, "a", null, "c"}; Arrays.sort(a, Comparators.compose(nullsLast, alphabetical)); assertArrayEquals(new String[] {"a", "b", "c", "d", null, null, null}, a); }
@Test public void testReverse() { Comparator<Long> ascending = new AscendingLongComparator(); Long[] a = new Long[] {5L, 1L, 3L, 2L, 4L, 1L}; Arrays.sort(a, ascending); assertArrayEquals(new Long[] {1L, 1L, 2L, 3L, 4L, 5L}, a); Comparator<Long> descending = Comparators.reverse(ascending); Arrays.sort(a, descending); assertArrayEquals(new Long[] {5L, 4L, 3L, 2L, 1L, 1L}, a); }