.sort()
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:
fivefouronesixthreetwo
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.