import java.util.Arrays;
class SortingDoubleArray {
public static void main(String[] args) {
// Creating a Double Array
double[] doubleArray = new double[] { 13.1, 2.5, 2.2, 41.1, 1.1 };
// Displaying Array before Sorting
System.out.println("**Double Array Before Sorting**");
for (double d: doubleArray){
System.out.println(d);
}
// Sorting the Array
Arrays.sort(doubleArray);
System.out.println("**Double Array After Sorting**");
for (double d: doubleArray){
System.out.println(d);
}
// Another Char Array
double[] doubleArray2 =
new double[] { 3, 12, 8, 101, 41, 99, 0};
// Selective Sorting
/* public static void sort(double[] 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(doubleArray2, 2, 5);
// Displaying array after selective sorting
System.out.println("**Selective Sorting**");
for (double d: doubleArray2){
System.out.println(d);
}
}
}
Output:
**Double Array Before Sorting** 13.1 2.5 2.2 41.1 1.1 **Double Array After Sorting** 1.1 2.2 2.5 13.1 41.1 **Selective Sorting** 3.0 12.0 8.0 41.0 101.0 99.0 0.0
References:
sort(double[] array)
sort(double[] array, int fromIndex, int toIndex)
Leave a Reply