Sort an Array in Descending Order in Java
Problem
Sorting an array in descending order means rearranging its elements from largest to smallest — the opposite of ascending order.
Given an array of integers, sort it in descending order.
Java Program
import java.util.Arrays;
public class SortDescending {
public static void main(String[] args) {
int[] arr = {8, 3, 10, 1, 6};
Arrays.sort(arr); // sort ascending first
int left = 0, right = arr.length - 1;
while (left < right) { // two-pointer reverse, same technique as reversing an array
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
System.out.println(Arrays.toString(arr));
}
}Output
Core Logic
Arrays.sort() only sorts primitives in ascending order — sorting ascending first, then reversing the result in place, gets to descending order in two simple steps.
- 1
Arrays.sort(arr)sorts the array in place into ascending order, exactly as it would for the ascending-order program. - 2Two pointers,
leftandright, start at the opposite ends of the now-sorted array. - 3The reverse loop swaps
arr[left]andarr[right], moving both pointers toward the middle, the same two-pointer technique used to reverse an array. - 4After the reverse, the array holds the same values as the ascending sort, but in the opposite order.
[8, 3, 10, 1, 6], sorting gives [1, 3, 6, 8, 10], and reversing that gives [10, 8, 6, 3, 1].Key Point: int[] has no direct 'sort descending' option in Arrays.sort() — that overload only exists for arrays of objects like Integer[], which is why primitives need this sort-then-reverse combination instead.
Why: The sort dominates the cost at O(n log n), and the reversal pass afterward is a single O(n) in-place pass using only a temp variable.
Key Concepts
Approach 2: Boxed Array with Comparator
import java.util.Arrays;
import java.util.Collections;
public class SortDescendingComparator {
public static void main(String[] args) {
Integer[] boxed = {8, 3, 10, 1, 6};
// Sorts using a comparator that reverses the natural ascending order
Arrays.sort(boxed, Collections.reverseOrder());
System.out.println(Arrays.toString(boxed));
}
}
Output
Core Logic
Arrays of boxed Integer objects, unlike primitive int[], can be sorted with a custom Comparator directly — including one that reverses the natural order in a single call.
- 1
Integer[] boxedholds boxedIntegerobjects instead of primitiveints, since comparators only work on object types. - 2
Arrays.sort(boxed, Collections.reverseOrder())sorts the array using a comparator that reverses the natural ascending order. - 3The result is directly in descending order — no separate reverse step is needed.
{8, 3, 10, 1, 6} boxed into Integer[], Arrays.sort(boxed, Collections.reverseOrder()) produces [10, 8, 6, 3, 1] in one call.Key Point: This trades the primitive-array approach's simplicity for the flexibility of comparator-based sorting — useful once the sort needs custom logic beyond plain descending order, but it comes with the memory overhead of boxing every value into an Integer.
Why: Boxing each primitive int into an Integer object allocates n wrapper objects, on top of the O(n log n) comparator-based sort.
Key Concepts
Approach 3: Java 8
import java.util.Arrays;
import java.util.Comparator;
public class SortDescendingStream {
public static void main(String[] args) {
int[] arr = {8, 3, 10, 1, 6};
// Box to sort with a comparator, then unbox back into a primitive array
int[] sorted = Arrays.stream(arr)
.boxed()
.sorted(Comparator.reverseOrder())
.mapToInt(Integer::intValue)
.toArray();
System.out.println(Arrays.toString(sorted));
}
}
Output
Core Logic
Boxing the stream, sorting it with a reversed comparator, then mapping back down to a primitive IntStream reproduces the same descending order without leaving a boxed Integer[] as the final result.
- 1
Arrays.stream(arr)opens anIntStreamover the primitive array. - 2
.boxed()converts it to aStream<Integer>, since comparator-based sorting needs an object type — the same reason the Boxed Array approach needsInteger[]. - 3
.sorted(Comparator.reverseOrder())sorts descending using the built-in reversing comparator. - 4
.mapToInt(Integer::intValue).toArray()unboxes the sorted stream back down into a plainint[].
[8, 3, 10, 1, 6], boxing, sorting descending, and unboxing back gives [10, 8, 6, 3, 1].Key Point: mapToInt(Integer::intValue) is what makes this different from the Boxed Array approach — that one finishes with a boxed Integer[], while this one boxes only temporarily to sort, then returns to a primitive int[] at the end.
Why: Sorting still costs O(n log n), and boxing every element temporarily for the comparator-based sort allocates n wrapper objects before mapToInt() converts back to primitives.