Java Arrays.sort() Method
Example
Sort an array of strings alphabetically:
String[] cars = {"Volvo", "BMW", "Tesla", "Ford", "Fiat", "Mazda", "Audi"};
Arrays.sort(cars);
Definition and Usage
The sort()
method sorts an array in
ascending order.
This method sorts arrays of strings alphabetically, and arrays of integers numerically.
Syntax
Arrays.sort(array)
Arrays.sort(array, start, end)
Parameter Values
Parameter | Description |
---|---|
array | Required. The array to be sorted |
start | Optional. The index position of the first element (inclusive) to be sorted |
end | Optional. The index position of the last element (exclusive) to be sorted |
Technical Details
Returns: | No return value |
---|---|
Java version: | 1.2 (java.util ) |
More Examples
Example
Sort an integer array numerically:
int[] myNum = {50, 10, 25, 1, 17, 99, 33};
Arrays.sort(myNum);
Example
Sort an integer array numerically, but only sort from index 1 to 3:
int[] myNum = {50, 10, 25, 1, 17, 99, 33};
// This will only sort the integers 10, 25, 1 and 17 from the myNum array
Arrays.sort(myNum, 1, 4);
Related Pages
❮ Arrays Methods