How to sort array using the arrays class in Java

3 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        double[] arr = {3.14, 3.141, 5.3, 8.4, 9.9, 2.2, 1.5, 4.6};
        
        java.util.Arrays.sort(arr);
        
        for (double d : arr) {
            System.out.println(d);
        }
    }
}


/*

run:

1.5
2.2
3.14
3.141
4.6
5.3
8.4
9.9

*/

 



answered Jul 21, 2019 by avibootz
edited Jul 21, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        double[] arr = {3.14, 3.141, 5.3, 8.4, 9.9, 2.2, 1.5, 4.6};
        
        java.util.Arrays.parallelSort(arr);
        
        for (double d : arr) {
            System.out.println(d);
        }
    }
}


/*

run:

1.5
2.2
3.14
3.141
4.6
5.3
8.4
9.9

*/

 



answered Jul 21, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        char[] arr = {'0', '3', 'E', 'e', 'b', 'B', 'a', 'A'};
        
        java.util.Arrays.sort(arr);
        
        for (char ch : arr) {
            System.out.println(ch);
        }
    }
}


/*

run:

0
3
A
B
E
a
b
e

*/

 



answered Jul 21, 2019 by avibootz
...