I can't use Arrays.sort() for Integer type array.
Is there any in-built method to sort Integer array?
Integer[] arr=new Integer[4]; //not "int"
I want to sort this arr array variable.
CodePudding user response:
Since Integer is a Comparable, you can use the overloaded Arrays.sort(Object[]). I.e.:
Integer[] arr = new Integer[4]; //not "int"
// fill in some data in arr
Arrays.sort(arr);
CodePudding user response:
The java.util.Arrays class has all required methods for sorting, including sort(Object[] a). Since Integer is an object, it will do nicely:
Integer[] arr = new Integer[] { 32, 12, 9, 77 };
Arrays.sort(arr);
// Array sorted it
