当前位置:网站首页>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 :
边栏推荐
- 生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
- Vs extension tool notes
- 高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
- Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
- Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
- Coreseek:第二步建索引及測试
- The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
- turbo intruder常用脚本
- Solution: prompt "unsupported video format" when inserting avi format video into the message
- B_ QuRT_ User_ Guide(36)
猜你喜欢
2022第六季完美童模陕西总决赛圆满落幕
七月第一周
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
leetcode-520. Detect capital letters -js
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
LDO稳压芯片-内部框图及选型参数
产业共融新势能,城链科技数字峰会厦门站成功举办
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
USB (XV) 2022-04-14
Matlab-SEIR传染病模型预测
随机推荐
Install Fedora under RedHat
Unity3d Learning Notes 6 - GPU instantiation (1)
Vs extension tool notes
USB(十五)2022-04-14
One week learning summary of STL Standard Template Library
CAIP2021 初赛VP
B_QuRT_User_Guide(40)
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
FPGA basics catalog
V-for traversal object
Happy gathering time
Entity层、DAO层、Service层、Controller层 先后顺序
2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
Archlinux install MySQL
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
USB (XVIII) 2022-04-17
伸展树(一) - 图文解析与C语言实现
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
v-for遍历对象
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」