Sort an array in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Java, sorting an array is usually a library problem, not an algorithm-writing problem. The main choice is which Arrays.sort overload matches your data: primitive values, objects with natural ordering, or objects sorted by a custom comparator.
Sort Primitive Arrays with Arrays.sort
For numeric and other primitive arrays, the standard answer is Arrays.sort(array). It sorts the array in place, which means the original array is modified rather than a new array being returned.
This is the normal production approach for ascending order. It is clearer and safer than reimplementing bubble sort, quicksort, or insertion sort by hand.
If you need only a portion of the array sorted, Java also provides range-based overloads. That can be useful when the array contains a sorted prefix and an unsorted working region.
Sort Object Arrays by Natural Order
Object arrays can also use Arrays.sort as long as the element type already has a natural ordering. Strings are a common example.
Here Java uses the compareTo implementation on String. The same rule applies to any class that implements Comparable.
Use a Comparator for Custom Ordering
When the natural order is not what you need, pass a comparator. This is the usual answer for descending order or sorting by one field of a custom class.
This keeps the sort rule close to the call site, which is often easier to maintain than embedding one global natural order into the class.
Descending Order and the Primitive Trap
A common source of confusion is that comparator-based sorting works only with object arrays, not primitive arrays. That means this works:
But the same approach does not work with int[]. If you need descending order for primitives, the simplest options are either to sort ascending and reverse manually or to store boxed values in Integer[] and use a comparator.
Prefer the Standard Library Over Hand-Written Sorts
Interview exercises often encourage implementing classic sorting algorithms. That is useful for learning time complexity, but it is rarely the correct answer in application code.
Using the standard library gives you tested behavior, better readability, and less maintenance risk. Unless you are writing a specialized low-level library, custom sort implementations are usually wasted effort.
Common Pitfalls
- Rewriting sorting algorithms manually when
Arrays.sortalready solves the problem. - Forgetting that sorting happens in place and mutates the original array.
- Trying to use a comparator with a primitive array such as
int[]. - Sorting custom objects without defining a comparator or
Comparableimplementation. - Converting arrays to lists only to sort them, even though the array API already supports the needed operation.
Summary
- Use
Arrays.sortfor nearly all array sorting in Java. - Primitive arrays use
Arrays.sort(array)for ascending order. - Object arrays can use natural ordering or a custom comparator.
- Descending comparator-based sorting requires object arrays such as
Integer[]. - Prefer the standard library over hand-written sorting code in production.
Related reading
- Sort an array of integers into odd, then even
- Sort array by firstname (alphabetically) in JavaScript
- Sort array in the minimum number of moves
- Sort array of days in javascript
- Sort array with with first half and second half sorted
- Sort ArrayList of custom Objects by property
- Sort objects in ArrayList by date?
- Sorted collection in Java

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.