How to Sort a Double Array in Java


The example aims to sort a double array by sorting method of Java.util package.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class SortDoubleArray {

	public static void main(String[] args) {
		
		//create double array.
		double[] d1 = new double[]{6.0, 120.0, 32.0, 24.0};
		
		System.out.println("Before sort, double array is : ");
		for(int index=0; index < d1.length ; index++)
		{
		      System.out.print("  "  + d1[index]);
		}
		
		/*
		 * To sort java double array use Arrays.sort() method of java.util package.
		 */
		Arrays.sort(d1);
		
		System.out.println();
		System.out.println("After sort, double array is :");
		for(int index=0; index < d1.length ; index++)
		{
		      System.out.print("  "  + d1[index]);
		}
	}

}

Output:

Before sort, double array is : 
  6.0  120.0  32.0  24.0
After sort, double array is :
  6.0  24.0  32.0  120.0

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments