Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

sort()

Example

Arrange an array of strings in alphabetical order.

String[] cars = {“Volvo”, “BMW”, “Tesla”, “Ford”, “Fiat”, “Mazda”, “Audi”};

Arrays.sort(cars);

Definition and Usage

The sort() method arranges an array in ascending order.

It sorts arrays of strings alphabetically and arrays of integers numerically.

Syntax

Arrays.sort(array)

 

Arrays.sort(array, start, end)

Parameter Values

Parameter

Description

array

Necessary: Rephrase the array intended for sorting.

start

Voluntary: Rephrase the starting index position of the first element (inclusive) to be sorted.

end

Voluntary: Rephrase the ending 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

Arrange an array of integers in numerical order.

int[] myNum = {50, 10, 25, 1, 17, 99, 33};
Arrays.sort(myNum);

Example

Sort an integer array numerically, limited to the elements 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);