当前位置:网站首页>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 :
边栏推荐
- In the field of software engineering, we have been doing scientific research for ten years!
- 欢聚时代一面
- 云原生数据仓库AnalyticDB MySQL版用户手册
- 给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
- USB (XVIII) 2022-04-17
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- Ros2 topic (03): the difference between ros1 and ros2 [02]
- Installing spss25
- re1攻防世界逆向
- System design overview
猜你喜欢
First week of July
B_ QuRT_ User_ Guide(37)
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
Deep understanding of MySQL lock and transaction isolation level
UE4_ Ue5 panoramic camera
UE4_UE5全景相机
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
js 获取对象的key和value
三问TDM
随机推荐
LeeCode -- 6. Z 字形变换
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
2022 届的应届生都找到工作了吗?做自媒体可以吗?
Bea-3xxxxx error code
B_QuRT_User_Guide(39)
Force deduction solution summary 648 word replacement
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Oracle-数据库的备份与恢复
ArcGIS: two methods of attribute fusion of the same field of vector elements
13、 System optimization
产业共融新势能,城链科技数字峰会厦门站成功举办
B_QuRT_User_Guide(36)
Matlab 信号处理【问答随笔·2】
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
谷歌浏览器怎么登录及开启同步功能
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
Unity3D学习笔记5——创建子Mesh
统计电影票房排名前10的电影并存入还有一个文件
Oracle database backup and recovery
Ros2 topic (03): the difference between ros1 and ros2 [02]