You could use this to sort all kind of Objects
sort(T[] a, Comparator<? super T> c)
Arrays.sort(a, Collections.reverseOrder());
Arrays.sort() cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort() method by passing reverse Comparator defined by Collections.reverseOrder() , it will throw the error
no suitable method found for sort(int[],comparator)
That will work fine with 'Array of Objects' such as Integer array but will not work with a primitive array such as int array.
The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays.
Answer from user173973 on Stack OverflowYou could use this to sort all kind of Objects
sort(T[] a, Comparator<? super T> c)
Arrays.sort(a, Collections.reverseOrder());
Arrays.sort() cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort() method by passing reverse Comparator defined by Collections.reverseOrder() , it will throw the error
no suitable method found for sort(int[],comparator)
That will work fine with 'Array of Objects' such as Integer array but will not work with a primitive array such as int array.
The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays.
for a list
Collections.sort(list, Collections.reverseOrder());
for an array
Arrays.sort(array, Collections.reverseOrder());
Sorting an int[] arr in reverse order
Sorting an int[] array in descending order
How to do descending order in selection sort program of Java....
Sorting array list without using .sort in Java
First of all sorry if this question may seem a bit trivial.
I googled a lot and could not find a suitable solution.
I want to sort an int[] arr in reverse order.
I cannot define comparator, cannot use Collections.reverseOrder or use lambda expression.
One post on stack overflow said that sort the array and then reverse it. Is there no simpler way?
Once again, I've been using java for about 4-5 months only and was using it for my DSA studies and this language is starting to frustrate me as much as C++.
Hello,
I am here after doing some research.
In this stack overflow article, it seems that they've settled on how to sort an array, but only in ascending order. Also, it is showed how to sort an Integer array in ascending order, but not an int[] array in ascending order, unless I am missing the answer. I'm looking for a way to sort an int[] array in descending order, without having to use while and for loops.
Sorry if this has been asked and answered before, I searched the sub and couldn't find it.
Here's my code so far:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {4,3,5,7,2,9,8,1,10,6};
sortArray(array);
}
public static void sortArray(int[] array) {
Arrays.sort(array);
for(int i=0; i<array.length; i++) {
System.out.println(array[i]);
}
}
} Edit, for posterity:
Thank you to a few different sources, u/PointB1ank being one of them.
Solution with comments:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
//Initiate array
int[] array = {4,3,5,7,2,9,8,1,10,6};
//Use java.util.Arrays; to sort array in ascending order
Arrays.sort(array);
//Use reverseArray method to reverse the array so it is now descending
reverseArray(array);
//Print the reversed array
for(int i = 0; i<array.length; i++){
System.out.println(array[i]);
}
}
public static int[] reverseArray(int[] array) {
//1st iteration: i = 0
//2nd iteration: i = 1
//3rd iteration: i = 2
//4th iteration: i = 3
//5th iteration: i = 4
for (int i = 0; i < array.length / 2; i++) {
//1st iteration: temp = 0
//2nd iteration: temp = 1
//3rd iteration: temp = 2
//4th iteration: temp = 3
//5th iteration: temp = 4
int temp = array[i];
//1st iteration: value at index 0 = value at index 10-0-1 or 9
//2nd iteration: value at index 1 = value at index 10-1-1 or 8
//3rd iteration: value at index 2 = value at index 10-2-1 or 7
//4th iteration: value at index 3 = value at index 10-3-1 or 6
//5th iteration: value at index 4 = value at index 10-4-1 or 5
array[i] = array[array.length - i - 1];
//1st iteration: value at index 10-0-1 = 0
//2nd iteration: value at index 10-1-1 = 1
//3rd iteration: value at index 10-2-1 = 2
//4th iteration: value at index 10-3-1 = 3
//5th iteration: value at index 10-4-1 = 4
array[array.length - i - 1] = temp;
}
//return array in descending order
return array;
}
}