How to sort elements in Float Array in Java?
Declaration :
1 |
public static void sort(float[] a) |
Explanation :
Purpose | The java.util.Arrays.sort(float[]) method sorts the specified array of floats into ascending numerical order. |
Parameters | a ===> This is the array to be sorted. |
Return Value | This method does not return any value. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.sort() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // Initialize unsorted float array float arrF[] = { 3.2f, 1.2f, 4.2f, 2.2f, 5.2f }; // Print the Unsorted Array List System.out.println("The Unsorted float array is:"); for (float num : arrF) { System.out.println("Array Element is " + num); } // Sort the array Arrays.sort(arrF); // Print the Unsorted Array List System.out.println("The sorted float array is:"); for (float num : arrF) { System.out.println("Array Element is " + num); } } } |
Output of Program :
1 2 3 4 5 6 7 8 9 10 11 12 |
The Unsorted float array is: Array Element is 3.2 Array Element is 1.2 Array Element is 4.2 Array Element is 2.2 Array Element is 5.2 The sorted float array is: Array Element is 1.2 Array Element is 2.2 Array Element is 3.2 Array Element is 4.2 Array Element is 5.2 |