If I reverse a string array, it works fine when using
Arrays.sort(arr, Collections.reverseOrder())
but why it does not work on char Array?
public static void main(String[] args) {
String[] temp = new String[] {"ab","aa","ac"};
Arrays.sort(temp, Collections.reverseOrder());
char[] arr = new char[]{'a','b','c'};
Arrays.sort(arr, Collections.reverseOrder());
}
CodePudding user response:
Collections.reverseOrder() won´t work on primitive types in java such as char type.
You will need to sort it and then reverse using for loop or put chars into string etc.
CodePudding user response:
You have an array of primitives. char
is a primitive. Collections.sort()
works on collections of objects.
To solve the problem do like this ...
Character[] arr = new Character[]{'a','b','c'};
Arrays.sort(arr, Collections.reverseOrder());
Instead of using a primitive type use its wrapper type, which is an object. Then, everything will work.
Does this solve your problem ? Tell me in the comments.