lukeb28 lukeb28 - 4 months ago 7
Java Question

Sorting an int array from highest to lowest

So I just learned about the

Arrays.sort(arrayName);
and applied it in a project of mine but I found it sorts from lowest to highest. Is there anyway to have it do the opposite? (I don't think I need to post the code in question but if it is needed I'll post it).

Answer

If you use an Integer[] instead of int[], then you can pass a Comparator as 2nd argument to the sort method. To impose reverse ordering, you can make use of Collections.reverseOrder() method:

Arrays.sort(arr, Collections.reverseOrder());
Comments