0Pricing
Java Academy · Lesson

Arrays.sort and Sorting

Sort primitive and object arrays.

Arrays.sort and Sorting is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why a Utility Class

Arrays in Java are objects, but they have very few built-in methods. The java.util.Arrays class fills the gap with static helper methods for sorting, searching, comparing, copying, and printing arrays.

  • Import it with import java.util.Arrays;
  • Every method is static, so you call them on the class itself: Arrays.sort(...)

Sorting a Primitive Array

Arrays.sort(int[]) sorts the array in place using a fast dual-pivot quicksort. There is no return value; the original array is rearranged.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {5, 2, 8, 1, 9, 3};
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
    }
}

Sorting Other Primitive Types

Overloads exist for every primitive type: double[], long[], char[], and more. They all sort ascending.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        double[] prices = {3.5, 1.2, 9.9, 0.5};
        Arrays.sort(prices);
        System.out.println(Arrays.toString(prices));
    }
}

Sorting a Range

You can sort only part of an array with Arrays.sort(array, fromIndex, toIndex). The range is inclusive of fromIndex and exclusive of toIndex.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {5, 2, 8, 1, 9, 3};
        Arrays.sort(nums, 1, 4);
        System.out.println(Arrays.toString(nums));
    }
}

Sorting Object Arrays

For object arrays such as String[], Arrays.sort uses the elements' natural ordering (their Comparable implementation). Strings sort lexicographically.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] names = {"Charlie", "Alice", "Bob"};
        Arrays.sort(names);
        System.out.println(Arrays.toString(names));
    }
}

Sorting with a Comparator

To override natural ordering, pass a Comparator. Comparator.reverseOrder() sorts descending. This only works on object arrays, not primitive arrays.

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

public class Main {
    public static void main(String[] args) {
        String[] names = {"Charlie", "Alice", "Bob"};
        Arrays.sort(names, Comparator.reverseOrder());
        System.out.println(Arrays.toString(names));
    }
}

Comparator by Length

A Comparator can compare on any property. Comparator.comparingInt(String::length) sorts strings from shortest to longest.

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

public class Main {
    public static void main(String[] args) {
        String[] words = {"banana", "fig", "apple"};
        Arrays.sort(words, Comparator.comparingInt(String::length));
        System.out.println(Arrays.toString(words));
    }
}

Stability

Sorting object arrays is stable: equal elements keep their original relative order. Primitive sorts are not guaranteed stable, but for primitives equal values are indistinguishable so it does not matter.

Parallel Sorting

For very large arrays, Arrays.parallelSort splits the work across multiple CPU cores. The API is identical to sort; the result is the same, only faster on big data.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {7, 3, 9, 1, 5, 2, 8, 4};
        Arrays.parallelSort(nums);
        System.out.println(Arrays.toString(nums));
    }
}

A Common Pitfall

Arrays.sort returns void. A line like int[] sorted = Arrays.sort(nums); will not compile. Sort first, then use the same variable.

Sorting Then Printing

A typical flow: sort the array in place, then print it with Arrays.toString to verify the result.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] scores = {88, 72, 95, 60, 81};
        Arrays.sort(scores);
        System.out.println("Lowest:  " + scores[0]);
        System.out.println("Highest: " + scores[scores.length - 1]);
        System.out.println(Arrays.toString(scores));
    }
}

Quick Check

Test your understanding of sorting.

Recap

You learned to sort arrays with the Arrays utility class.

  • Arrays.sort(arr) sorts primitives or objects in place, ascending.
  • A range overload sort(arr, from, to) sorts part of the array.
  • Object arrays accept a Comparator for custom ordering.
  • parallelSort speeds up large arrays.

Frequently asked questions

Is the “Arrays.sort and Sorting” lesson free?

Yes — the full text of “Arrays.sort and Sorting” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arrays.sort and Sorting”?

Sort primitive and object arrays. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arrays.sort and Sorting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays.sort and Sorting
  2. Arrays.binarySearch
  3. Arrays.fill and copyOf
  4. Arrays.equals and toString
← Back to Java Academy