Sorting float array in Java example

In this tutorial we are gonna learn how to sort a float array.

import java.util.Arrays;

class SortingFloatArrayExample {

  public static void main(String[] args) {
 
    // Creating a Float Array
    float[] floatArray = 
       new float[] { 21.1f, 9.9f, 9.8f, 7.5f, 2.1f };
 
    // Displaying Array before Sorting
    System.out.println("**Float Array Before Sorting**");
    for (float temp: floatArray){
       System.out.println(temp);
    }

    // Sorting the Array
    Arrays.sort(floatArray);
    System.out.println("**Float Array After Sorting**");
    for (float temp: floatArray){
       System.out.println(temp);
    }
 
    // Another Char Array
    float[] floatArray2 = 
       new float[] { 1.9f, 19.9f, 9.8f, 27.5f, 2.1f, 3.5f, 1.1f};
 
    // Selective Sorting
    /* public static void sort(float[] a, int fromIndex,
     * int toIndex): Sorts the specified range of the 
     * array into ascending order. The range to be sorted 
     * extends from the index fromIndex, inclusive, to the 
     * index toIndex, exclusive. If fromIndex == toIndex, 
     * the range to be sorted is empty.
     */
    Arrays.sort(floatArray2, 2, 5);

    // Displaying array after selective sorting 
    System.out.println("**Selective Sorting**");
    for (float temp: floatArray2){
       System.out.println(temp);
    }
  }
}

Output:

**Float Array Before Sorting**
21.1
9.9
9.8
7.5
2.1
**Float Array After Sorting**
2.1
7.5
9.8
9.9
21.1
**Selective Sorting**
1.9
19.9
2.1
9.8
27.5
3.5
1.1

References:
sort(float[] array) method – javadoc
sort(float[] array, int fromIndex, int toIndex) method – javadoc

READ:   Get #CyberAware For National Cyber Security Awareness Month 2016