- Java.util - Home
 - Java.util - ArrayDeque
 - Java.util - ArrayList
 - Java.util - Arrays
 - Java.util - BitSet
 - Java.util - Calendar
 - Java.util - Collections
 - Java.util - Currency
 - Java.util - Date
 - Java.util - Dictionary
 - Java.util - EnumMap
 - Java.util - EnumSet
 - Java.util - Formatter
 - Java.util - GregorianCalendar
 - Java.util - HashMap
 - Java.util - HashSet
 - Java.util - Hashtable
 - Java.util - IdentityHashMap
 - Java.util - LinkedHashMap
 - Java.util - LinkedHashSet
 - Java.util - LinkedList
 - Java.util - ListResourceBundle
 - Java.util - Locale
 - Java.util - Observable
 - Java.util - PriorityQueue
 - Java.util - Properties
 - Java.util - PropertyPermission
 - Java.util - PropertyResourceBundle
 - Java.util - Random
 - Java.util - ResourceBundle
 - Java.util - ResourceBundle.Control
 - Java.util - Scanner
 - Java.util - ServiceLoader
 - Java.util - SimpleTimeZone
 - Java.util - Stack
 - Java.util - StringTokenizer
 - Java.util - Timer
 - Java.util - TimerTask
 - Java.util - TimeZone
 - Java.util - TreeMap
 - Java.util - TreeSet
 - Java.util - UUID
 - Java.util - Vector
 - Java.util - WeakHashMap
 
- Java.util - Interfaces
 - Java.util - Exceptions
 - Java.util - Enumerations
 - Java.util Useful Resources
 - Java.util - Useful Resources
 - Java.util - Discussion
 
Java Arrays sort(double[] a) Method
Description
The Java Arrays sort(double[] a) method sorts the specified array of doubles into ascending numerical order. This method using a Dual-Pivot Quicksort algorithm which breaks the array into subarrays, sorted them and then merged to give a sorted array.
Declaration
Following is the declaration for java.util.Arrays.sort(double[] a) method
public static void sort(double[] a)
Parameters
a − This is the array to be sorted.
Return Value
This method does not return any value.
Exception
NA
Java Arrays sort(double[] a, int fromIndex, int toIndex) Method
Description
The Java Arrays sort(double[] a, int fromIndex, int toIndex) method sorts the specified range of given array of doubles into ascending numerical order. This method using a Dual-Pivot Quicksort algorithm which breaks the array into subarrays, sorted them and then merged to give a sorted array.
Declaration
Following is the declaration for java.util.Arrays.sort(double[] a, int fromIndex, int toIndex) method
public static void sort(double[] a, int fromIndex, int toIndex)
Parameters
a − This is the array to be sorted.
fromIndex − This is the index of the first element, inclusive, to be sorted.
toIndex − This is the index of the last element, exclusive, to be sorted
Return Value
This method is not returning anything.
Exception
IllegalArgumentException − if fromIndex > toIndex
ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > array.length
Sorting an Array of doubles Example
The following example shows the usage of Java Arrays sort(double[]) method. First, we've created an array of doubles, printed the original array. Using sort() method, array is sorted and printed thereafter.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      double arr[] = { 11.0, 54.0, 23.0, 32.0, 15.0, 24.0, 31.0, 12.0 };
      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort the array
      Arrays.sort(arr);
      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [11.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ] Sorted Array: [11.0 12.0 15.0 23.0 24.0 31.0 32.0 54.0 ]
Sorting an Array of doubles using Range Example
The following example shows the usage of Java Arrays sort(double[], int, int) method. First, we've created an array of doubles, printed the original array. Using sort() method, array is sorted and printed thereafter.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      double arr[] = { 11.0, 54.0, 23.0, 32.0, 15.0, 24.0, 31.0, 12.0 };
      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort the array
      Arrays.sort(arr, 0, arr.length);
      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [11.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ] Sorted Array: [11.0 12.0 15.0 23.0 24.0 31.0 32.0 54.0 ]
Sorting Sub-Array of doubles Example
The following example shows the usage of Java Arrays sort(double[], int, int) method. First, we've created an array of doubles, printed the original array. Using sort() method, a sub-array is sorted and printed thereafter.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      double arr[] = { 11.0, 54.0, 23.0, 32.0, 15.0, 24.0, 31.0, 12.0 };
      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort first five elements of the array 
      Arrays.sort(arr, 0, 5);
      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [11.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ] Sorted Array: [11.0 15.0 23.0 32.0 54.0 24.0 31.0 12.0 ]