当前位置:网站首页>List. How to achieve ascending and descending sort() 2020.8.6
List. How to achieve ascending and descending sort() 2020.8.6
2022-07-07 23:28:00 【codepig16】
The following code is directed to a list Add three numbers to the set , Then in ascending order 、 Descending order , Print .
Be careful :o1 Represents the next number ,o2 Represents the previous number ,-1 Represents the swap position ,1 Indicates no exchange
@Test
public void test() {
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(1);
list.add(2);
print_list(list);
// Descending order
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//o1 Represents the next number o2 Represents the previous number
if (o1 > o2)return -1; //-1 Indicates the position of exchanging two numbers So here is a descending order
else return 1;
}
});
print_list(list);
// Ascending order
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 < o2) return -1; //-1 Represents the position of exchanging two numbers
else return 1;
}
});
print_list(list);
}
public void print_list(List<Integer> list) {
for (Integer i : list) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
}
The printing result is shown in the figure below :

边栏推荐
猜你喜欢

Inftnews | the wide application of NFT technology and its existing problems

Cloud native is devouring everything. How should developers deal with it?

经纬度PLT文件格式说明

高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏

UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)

B_ QuRT_ User_ Guide(37)

【编译原理】词法分析设计实现

UE4_ Ue5 panoramic camera

Deep understanding of MySQL lock and transaction isolation level

Description of longitude and latitude PLT file format
随机推荐
云原生数据仓库AnalyticDB MySQL版用户手册
First week of July
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
php 使用阿里云存储
Turbo introder common scripts
Sequence of entity layer, Dao layer, service layer and controller layer
648. 单词替换
PCB wiring rules of PCI Express interface
POJ2392 SpaceElevator [DP]
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
Mysql索引优化实战二
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
Markdown
V-for traversal object
USB (十八)2022-04-17
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
B_QuRT_User_Guide(39)
ArcGIS: two methods of attribute fusion of the same field of vector elements
Inftnews | the wide application of NFT technology and its existing problems
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process