Sort an Array of Integers using Arrays.sort() Method Example

In this source code example, we will see how to sort an array of integers using Arrays.sort() method with an example.

Sorting an array of integers – Arrays.sort() method

package com.java.array.tutorial.sorting;

import java.util.Arrays;

public class SortingArrayExamples {
    public static void main(String[] args) {
        // Sorting an array of primitives – Arrays.sort() method
        int[] myArray = {
            10,
            30,
            20,
            50,
            40
        };
        System.out.println("Before sorting => " + Arrays.toString(myArray));
        Arrays.sort(myArray);
        System.out.println("After sorting => " + Arrays.toString(myArray));
    }
}
Output:
Before sorting => [10, 30, 20, 50, 40]
After sorting => [10, 20, 30, 40, 50]

Reference



Comments