+ 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 ?
10 Antworten
+ 6
@prashant this is because the method you are using only support wrapper classes. this is the reason.
+ 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]);
}
}
+ 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]);
}
}
+ 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 :)
+ 1
do this...
Integer[] a =new Integer[]{30,7,9,20};
Arrays.sort(a, Collections.reverseOrder());
+ 1
@farshaad heydari
I want use int not Integer
+ 1
@Alihan Demir
you code is for ascending order not for descending order
+ 1
again wrong code and wrong output @ Alihan
+ 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
+ 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!)