+ 1

The following code will sort the array in ascending order :

int a[] = {30,7,9,20}; Arrays.sort(a); System.out.println(Arrays.toString(a)); How i sort in Descending order ?

22nd Feb 2018, 9:11 AM
Prashant Kumar
Prashant Kumar - avatar
10 Respuestas
+ 6
@prashant this is because the method you are using only support wrapper classes. this is the reason.
22nd Feb 2018, 3:39 PM
shobhit
shobhit - avatar
+ 17
import java.util.Arrays;//you need to import arrays public class Program { public static void main(String [] args) int [] a = {30,7,9,20};//wrong array define Arrays.sort(a); for(int i=0;i<a.length;i++) System.out.println(a[i]); } }
22nd Feb 2018, 9:27 AM
Ali Han Demir
Ali Han Demir - avatar
+ 17
here is desc code import java.util.Arrays; public class Program { public static void main(String [] args) { int [] a = {30,7,9,20}; for(int i=0;i<a.length;i++) System.out.println(Arrays.toString(a)); for(int i=0;i<a.length;i++) System.out.println(a[i]); } }
22nd Feb 2018, 10:14 AM
Ali Han Demir
Ali Han Demir - avatar
+ 17
I finally did it:) import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { Integer[] a = new Integer[] { new Integer(30), new Integer(7), new Integer(9), new Integer(20) }; Arrays.sort(a, Collections.reverseOrder()); for (Integer i : a) { System.out.println(i.intValue()); } } } ıt cannot sort arrays of primitives this is why ı used integer :)
22nd Feb 2018, 12:41 PM
Ali Han Demir
Ali Han Demir - avatar
+ 1
do this... Integer[] a =new Integer[]{30,7,9,20}; Arrays.sort(a, Collections.reverseOrder());
22nd Feb 2018, 9:48 AM
Farshaad Heydari
Farshaad Heydari - avatar
+ 1
@farshaad heydari I want use int not Integer
22nd Feb 2018, 9:50 AM
Prashant Kumar
Prashant Kumar - avatar
+ 1
@Alihan Demir you code is for ascending order not for descending order
22nd Feb 2018, 9:53 AM
Prashant Kumar
Prashant Kumar - avatar
+ 1
again wrong code and wrong output @ Alihan
22nd Feb 2018, 10:23 AM
Prashant Kumar
Prashant Kumar - avatar
+ 1
thanx , I know that we can't sort array of ing type but we can sort other primitive type of array . I was searching why we can't sort int type only
22nd Feb 2018, 12:56 PM
Prashant Kumar
Prashant Kumar - avatar
+ 1
sorry for answering to late ...., so if u want to sort an array of int s(primitives). u could use the selection sort or insertion sort algorithm. but if you are very beginner in Java u might have some troubles to understand an write them(like me!)
22nd Feb 2018, 6:50 PM
Farshaad Heydari
Farshaad Heydari - avatar