Java Utility Library

Java Arrays - sort() Method



The java.util.Arrays.sort() method is used to sort the specified array into ascending numerical order.

Syntax

public static void sort(double[] a)

Parameters

a Specify the array to be sorted.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Arrays.sort() method is used to sort a specified double array.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an unsorted double array
    double MyArr[] = {10.1, 2.34, -3.6, 35.01, 56.23};

    //printing array before sorting
    System.out.print("MyArr contains:"); 
    for(double i: MyArr)
      System.out.print(" " + i);

    //sort the array
    Arrays.sort(MyArr);

    //printing array after sorting
    System.out.print("\nMyArr contains:"); 
    for(double i: MyArr)
      System.out.print(" " + i);   
  }
}

The output of the above code will be:

MyArr contains: 10.1 2.34 -3.6 35.01 56.23
MyArr contains: -3.6 2.34 10.1 35.01 56.23

❮ Java.util - Arrays