arrays.sort倒序
时间: 2023-10-12 16:04:11 浏览: 92
要将数组按照倒序进行排序,可以使用Arrays.sort()方法结合Comparator.reverseOrder()。示例代码如下:
```java
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSort {
public static void main(String[] args) {
Integer[] a = {2, 5, 4, 3, 1, 8};
Arrays.sort(a, Comparator.reverseOrder());
System.out.println(Arrays.toString(a));
}
}
```
运行结果为:。在这个示例中,我们使用了Integer类型的数组,并使用Comparator.reverseOrder()作为排序比较器,实现了倒序排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Arrays.sort()的用法 数组倒序降序的方法使用](https://blog.csdn.net/Inoue1595505/article/details/125205868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [[转载]对数组进行正序排序和倒序排序。](https://blog.csdn.net/ABoLuoTaMei/article/details/84985502)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文