Array Sorting

Su Suriya Ravichandran Updated 14 Nov 2026
7 min read ·Lesson 69 of 306

Sorting arranges the elements of an array in order: numbers from smallest to largest, text alphabetically, objects by price or date or any rule you define. Java's java.util.Arrays class provides fast, well-tested sorting methods, so you rarely need to write a sorting algorithm yourself. What you do need to understand is how ordering is defined for different types, and how to specify your own order.

Sorting Primitive Arrays

import java.util.Arrays;

int[] numbers = {42, 7, 19, 3, 25};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));   // [3, 7, 19, 25, 42]

double[] prices = {499.0, 89.5, 1299.0, 249.99};
Arrays.sort(prices);
System.out.println(Arrays.toString(prices));    // [89.5, 249.99, 499.0, 1299.0]

char[] letters = {'d', 'a', 'c', 'b'};
Arrays.sort(letters);
System.out.println(new String(letters));        // abcd

Key facts:

  • Arrays.sort sorts in place: it rearranges the array you pass and returns nothing (void). If you need the original order too, sort a copy (int[] sorted = numbers.clone(); Arrays.sort(sorted);).
  • Primitive arrays are sorted in ascending numeric order. For char, that is the order of the character codes.
  • For primitives, Java uses a Dual-Pivot Quicksort, which runs in O(n log n) time on average and is very fast in practice.

Sorting part of an array

Overloads take a range: the start index is inclusive and the end index is exclusive.

int[] n = {5, 3, 9, 1, 7};
Arrays.sort(n, 1, 4);                     // sorts indexes 1, 2, 3 only
System.out.println(Arrays.toString(n));   // [5, 1, 3, 9, 7]

Descending order for primitives

Arrays.sort has no comparator parameter for primitive arrays. Two common approaches:

// 1. Sort ascending, then reverse in place
int[] a = {5, 3, 9, 1, 7};
Arrays.sort(a);
for (int i = 0, j = a.length - 1; i < j; i++, j--) {
    int tmp = a[i]; a[i] = a[j]; a[j] = tmp;
}
// [9, 7, 5, 3, 1]

// 2. Use a boxed array, which accepts a comparator
Integer[] b = {5, 3, 9, 1, 7};
Arrays.sort(b, Comparator.reverseOrder());
// [9, 7, 5, 3, 1]

The first approach avoids boxing and is better for large arrays of numbers.

Sorting Strings

String implements Comparable, so an array of strings sorts in its natural order:

String[] words = {"banana", "apple", "Cherry"};
Arrays.sort(words);
System.out.println(Arrays.toString(words));   // [Cherry, apple, banana]

Natural string order compares character codes, and every uppercase letter (A–Z, codes 65–90) comes before every lowercase letter (a–z, codes 97–122). That is why "Cherry" sorts first. For a case-insensitive alphabetical order, pass a comparator:

Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);
System.out.println(Arrays.toString(words));   // [apple, banana, Cherry]

For language-correct ordering of accented or non-English text, use java.text.Collator:

Arrays.sort(words, java.text.Collator.getInstance(java.util.Locale.ENGLISH));

Sorting Objects: Comparable

An array of objects can be sorted with Arrays.sort(array) only if the element class implements Comparable, which defines its natural order:

public class Student implements Comparable<Student> {
    private final String name;
    private final int rollNo;

    public Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }

    @Override
    public int compareTo(Student other) {
        return Integer.compare(this.rollNo, other.rollNo);   // order by roll number
    }

    @Override
    public String toString() { return rollNo + ":" + name; }
}
Student[] cls = { new Student("Ada", 3), new Student("Linus", 1), new Student("Grace", 2) };
Arrays.sort(cls);
System.out.println(Arrays.toString(cls));   // [1:Linus, 2:Grace, 3:Ada]

compareTo returns a negative number if this comes first, zero if they are equal in order, and a positive number if this comes after other. Use Integer.compare, Double.compare, or String.compareTo rather than subtracting values: this.rollNo - other.rollNo can overflow for large or negative numbers and give the wrong sign.

Sorting objects whose class does not implement Comparable throws ClassCastException at runtime. Comparable has its own lesson.

Sorting Objects: Comparator

A Comparator defines an order from outside the class. It lets you sort the same objects in many different ways without changing the class:

record Product(String name, String category, double price, double rating) { }

Product[] products = {
    new Product("Keyboard", "Accessories", 2499, 4.4),
    new Product("Monitor", "Displays", 12999, 4.6),
    new Product("Mouse", "Accessories", 799, 4.4)
};

Arrays.sort(products, Comparator.comparingDouble(Product::price));            // cheapest first
Arrays.sort(products, Comparator.comparing(Product::name));                    // by name
Arrays.sort(products, Comparator.comparingDouble(Product::price).reversed());  // most expensive first

The factory methods on Comparator build comparators from the property to compare:

MethodMeaning
Comparator.comparing(keyExtractor)Order by a Comparable key such as a String or LocalDate
comparingInt / comparingLong / comparingDoubleOrder by a primitive key, avoiding boxing
.reversed()Reverse the whole order
.thenComparing(...)Tie-breaker used when the previous comparison is equal
Comparator.naturalOrder() / reverseOrder()Natural order of Comparable elements, or its reverse
Comparator.nullsFirst(cmp) / nullsLast(cmp)Handle null elements instead of throwing

Multiple sort keys

Arrays.sort(products, Comparator
        .comparingDouble(Product::rating).reversed()     // highest rated first
        .thenComparingDouble(Product::price));           // then cheapest first

Note that reversed() applies to the entire comparator built so far. Writing it before thenComparing reverses only the rating, which is what is wanted here. Comparators have their own lesson.

Sorting rows of a 2D array

Each row of an int[][] is an object (int[]), so rows can be sorted with a comparator on a chosen column:

int[][] results = { {3, 90}, {1, 75}, {2, 90} };   // {studentId, score}

Arrays.sort(results, Comparator.comparingInt((int[] row) -> row[1]).reversed()
                               .thenComparingInt(row -> row[0]));

System.out.println(Arrays.deepToString(results));   // [[2, 90], [3, 90], [1, 75]]

The explicit type (int[] row) in the first lambda helps the compiler infer types through the chained reversed() call.

Stability

A sort is stable if elements that compare as equal keep their original relative order. Java's object sort (a TimSort variant) is stable. This allows sorting by multiple criteria in separate passes: sort by the secondary key first, then by the primary key, and ties in the primary key remain ordered by the secondary one.

Primitive sorts are not documented as stable, but for primitives the question does not arise: two equal int values are indistinguishable.

Nulls

Sorting an object array containing null with natural ordering throws NullPointerException. Wrap the comparator to decide where nulls go:

String[] tags = {"java", null, "bash", "c"};
Arrays.sort(tags, Comparator.nullsLast(Comparator.naturalOrder()));
System.out.println(Arrays.toString(tags));   // [bash, c, java, null]

Parallel Sorting

For very large arrays, Arrays.parallelSort splits the work across CPU cores using the common fork/join pool:

long[] big = new java.util.Random(42).longs(5_000_000).toArray();
Arrays.parallelSort(big);

It produces the same result as Arrays.sort. For small arrays the overhead of coordinating threads makes it no faster, so use it only when profiling shows sorting is a bottleneck on large data.

Sorting Collections

The same ideas apply to lists:

List<String> names = new ArrayList<>(List.of("Linus", "Ada", "Grace"));
names.sort(Comparator.naturalOrder());       // or Collections.sort(names)
names.sort(Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder()));

List.sort copies the list into an array, sorts it with the same algorithm, and writes the elements back.

Searching Requires Sorting

Binary search (Arrays.binarySearch) only works on a sorted array, and it must be sorted with the same ordering used for the search. Sorting once and then searching many times is far faster than scanning an unsorted array repeatedly. The next lesson covers searching.

Practical Example: Product Listing

An online store shows products ordered by rating, and within the same rating, by price. A second view groups them by category while preserving that order.

import java.util.Arrays;
import java.util.Comparator;

public class ProductListing {
    record Product(String name, String category, double price, double rating) { }

    public static void main(String[] args) {
        Product[] products = {
            new Product("Keyboard", "Accessories", 2499, 4.4),
            new Product("Monitor", "Displays", 12999, 4.6),
            new Product("Mouse", "Accessories", 799, 4.4),
            new Product("Webcam", "Accessories", 2999, 4.1),
            new Product("Laptop Stand", "Accessories", 1299, 4.6)
        };

        Arrays.sort(products, Comparator
                .comparingDouble(Product::rating).reversed()
                .thenComparingDouble(Product::price));

        for (Product p : products) {
            System.out.printf("%-13s %4.1f %9.2f%n", p.name(), p.rating(), p.price());
        }

        Arrays.sort(products, Comparator.comparing(Product::category));
        System.out.println();
        for (Product p : products) {
            System.out.println(p.category() + " " + p.name());
        }
    }
}

Output:

Laptop Stand   4.6   1299.00
Monitor        4.6  12999.00
Mouse          4.4    799.00
Keyboard       4.4   2499.00
Webcam         4.1   2999.00

Accessories Laptop Stand
Accessories Mouse
Accessories Keyboard
Accessories Webcam
Displays Monitor

The first sort orders by rating, highest first, and breaks ties by price, cheapest first: the two 4.6 products appear as Laptop Stand then Monitor, and the two 4.4 products as Mouse then Keyboard. The second sort orders only by category. Because Java's object sort is stable, products in the same category keep the order from the first sort, so within "Accessories" they are still listed by rating and then price. This two-pass technique is a direct, practical consequence of stability.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Array Sorting and get answers drawn from it.

Signed-in readers only.