.sort()

StevenSwiniarski's avatar
Published May 25, 2022
Contribute to Docs

The .sort() method of the Arrays class sorts a given array into ascending order.

Syntax

Arrays.sort(array)

Arrays.sort(array, start, end)

Where array is the array being sorted. start and end indices can also be specified and the sort will be confined to the given range.

Example

The following example creates an array and then sorts it.

import java.util.*;
public class Example {
public static void main(String args[]) {
String[] a = {"one", "two", "three", "four", "five", "six"};
Arrays.sort(a);
for (int i = 0; i < 6; i++) {
System.out.println(a[i]);
}
}
}

This will output the following:

five
four
one
six
three
two

All contributors

Contribute to Docs

Learn Java on Codecademy