arrays.sort
时间: 2023-10-30 22:40:06 浏览: 76
() method in Java is used to sort the elements of an array in ascending or descending order. The syntax of the sort() method is:
Arrays.sort(arrayName);
where arrayName is the name of the array that needs to be sorted.
If the elements of the array are of a primitive data type, such as int or double, then the sort() method sorts the elements in ascending order.
If the elements of the array are of a reference data type, such as String or Integer, then the sort() method sorts the elements in lexicographic order, which means the elements are sorted based on their ASCII values.
To sort the elements in descending order, we can use the reverseOrder() method of the Collections class, which returns a Comparator object that can be used to sort the elements in descending order. The syntax is:
Arrays.sort(arrayName, Collections.reverseOrder());
where arrayName is the name of the array that needs to be sorted in descending order.
阅读全文