Java ProgramsArraysSort an Array in Descending Order

Sort an Array in Descending Order in Java

beginner·  Arrays  ·  Sorting

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.

Input
[8, 3, 10, 1, 6]
Output
[10, 8, 6, 3, 1]

Java Program

Java
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

[10, 8, 6, 3, 1]

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.

How It Works
  1. 1Arrays.sort(arr) sorts the array in place into ascending order, exactly as it would for the ascending-order program.
  2. 2Two pointers, left and right, start at the opposite ends of the now-sorted array.
  3. 3The reverse loop swaps arr[left] and arr[right], moving both pointers toward the middle, the same two-pointer technique used to reverse an array.
  4. 4After the reverse, the array holds the same values as the ascending sort, but in the opposite order.
For [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.

Complexity
Time Complexity: O(n log n)Space Complexity: O(1)

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

Arrays.sort()two-pointer reverse

Approach 2: Boxed Array with Comparator

Java
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

[10, 8, 6, 3, 1]

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.

How It Works
  1. 1Integer[] boxed holds boxed Integer objects instead of primitive ints, since comparators only work on object types.
  2. 2Arrays.sort(boxed, Collections.reverseOrder()) sorts the array using a comparator that reverses the natural ascending order.
  3. 3The result is directly in descending order — no separate reverse step is needed.
For {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.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

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

Integer[]Collections.reverseOrder()boxing

Approach 3: Java 8

Java
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

[10, 8, 6, 3, 1]

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.

How It Works
  1. 1Arrays.stream(arr) opens an IntStream over the primitive array.
  2. 2.boxed() converts it to a Stream<Integer>, since comparator-based sorting needs an object type — the same reason the Boxed Array approach needs Integer[].
  3. 3.sorted(Comparator.reverseOrder()) sorts descending using the built-in reversing comparator.
  4. 4.mapToInt(Integer::intValue).toArray() unboxes the sorted stream back down into a plain int[].
For [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.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

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.

Key Concepts

StreamIntStreamComparator.reverseOrder()mapToInt()

Related Programs