With Eclipse Collections you could create a primitive double list, sort it and then reverse it to put it in descending order. This approach would avoid boxing the doubles.
MutableDoubleList doubleList =
DoubleLists.mutable.with(
0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71,
0.71, 0.71, 0.92, 0.12, 0.65, 0.34, 0.62)
.sortThis().reverseThis();
doubleList.each(System.out::println);
Updated: Sept. 2023
Since the 10.3 release, Eclipse Collections supports indirect sorting of primitive lists.
@Test
public void sortDoubleListInDescendingOrder()
{
MutableDoubleList testList = DoubleLists.mutable
.with(0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71, 0.71,
0.71, 0.92, 0.12, 0.65, 0.34, 0.62);
DoubleList expected = DoubleLists.mutable
.with(0.92, 0.9, 0.71, 0.71, 0.71, 0.65, 0.62, 0.54,
0.5, 0.34, 0.2, 0.12, 0.1, 0.1, 0.1);
Assertions.assertNotEquals(expected, testList);
testList.sortThis((i, j) -> -Double.compare(i, j));
Assertions.assertEquals(expected, testList);
}
End Update
If you want a List<Double>
, then the following would work.
List<Double> objectList =
Lists.mutable.with(
0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71,
0.71, 0.71, 0.92, 0.12, 0.65, 0.34, 0.62)
.sortThis(Collections.reverseOrder());
objectList.forEach(System.out::println);
If you want to keep the type as ArrayList<Double>
, you can initialize and sort the list using the ArrayListIterate
utility class as follows:
ArrayList<Double> arrayList =
ArrayListIterate.sortThis(
new ArrayList<>(objectList), Collections.reverseOrder());
arrayList.forEach(System.out::println);
Note: I am a committer for Eclipse Collections.