当前位置:网站首页>Objects in the list, sorted by a field
Objects in the list, sorted by a field
2022-07-05 21:15:00 【Try to survive】
https://www.cnblogs.com/masterpick/p/13267764.html
public class Logs implements Serializable, Comparable<Logs> {
private String id;
private String username;
private String contents;
private String consequence;
private String ip;
private Long times;
@Override
public int compareTo(Logs o) {
return 0;
}
}
analysis : Logs Entity , Inheritance Comparable<T> Interface , And then one more public int compareTo(Logs o) Method .
Now? , I want to be right List<Logs> Data in press times Field to sort
@Test
public void testMath(){
List<Logs> list = new ArrayList<Logs>();
list.add(new Logs("1","chen", "djifand", "jdifa", "jdfid", 49L));
list.add(new Logs("134","ce", "cae", "jdifa", "jdfid", 123L));
list.add(new Logs("3","cet", "djifand", "jdifa", "jdfid", 99L));
list.add(new Logs("4","egge", "djifand", "jdifa", "jdfid", 111L));
list.add(new Logs("15","chen", "djifand", "jdifa", "jdfid", 59L));
list.add(new Logs("6","chen2", "cae", "jdifa", "jdfid", 123L));
list.add(new Logs("87","cet", "djifand", "jdifa", "jdfid", 213L));
list.add(new Logs("8","egge", "djifand", "jdifa", "jdfid", 201L));
list.add(new Logs("9","egge2", "djifand", "jdifa", "jdfid", 269L));
list.add(new Logs("10","ce", "cae", "jdifa", "jdfid", 121L));
list.add(new Logs("11","chen3", "djifand", "jdifa", "jdfid", 1123L));
list.add(new Logs("5","cet2", "djifand", "jdifa", "jdfid", 12L));
System.out.println("---- Output before sorting ----- ");
for (Logs logs : list) {
System.out.println(" data : "+logs);
}
System.out.println("---- The result after sorting ----- ");
List<Logs> search = search(list);
for (Logs logs : search) {
System.out.println("-->"+logs);
}
}
// The way 1: Descending order
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
if ((o1.getTimes() > o2.getTimes())){
return -1;
}
if (o1.getTimes() == o2.getTimes()){
return 0;
}
return 1;
}
});
return logsList;
}
// The way 2: Ascending order
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
if ((o1.getTimes() > o2.getTimes())){
return 1;
}
if (o1.getTimes() == o2.getTimes()){
return 0;
}
return -1;
}
});
return logsList;
}
// The way 3: Descending order
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
return -o1.getTimes().compareTo(o2.getTimes());
}
});
return logsList;
}
// The way 4: Ascending order
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
return o1.getTimes().compareTo(o2.getTimes());
}
});
return logsList;
}
边栏推荐
- 水泥胶黏剂BS 476-4 不燃性测试
- js常用方法封装
- PVC plastic sheets BS 476-6 determination of flame propagation properties
- XML modeling
- Wood board ISO 5660-1 heat release rate mapping test
- shell编程100例
- Write an interface based on flask
- AITM2-0002 12s或60s垂直燃烧试验
- Influence of oscilloscope probe on signal source impedance
- 模式-“里氏替换原则”
猜你喜欢
![[case] Application of positioning - Taobao rotation map](/img/2d/c834ce95a2c8e53a20e67fa2e99439.png)
[case] Application of positioning - Taobao rotation map

EN 438-7建筑覆盖物装饰用层压板材产品—CE认证

PVC 塑料片BS 476-6 火焰传播性能测定

Wood board ISO 5660-1 heat release rate mapping test

Write an interface based on flask

请查收.NET MAUI 的最新学习资源

【案例】定位的运用-淘宝轮播图

EN 438-7 laminated sheet products for building covering decoration - CE certification

leetcode:1755. Sum of subsequences closest to the target value

MySQL InnoDB Architecture Principle
随机推荐
PVC 塑料片BS 476-6 火焰传播性能测定
SYSTEMd resolved enable debug log
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
请查收.NET MAUI 的最新学习资源
CLion配置visual studio(msvc)和JOM多核编译
Hdu2377bus pass (build more complex diagram +spfa)
Selenium gets the verification code image in DOM
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
hdu2377Bus Pass(构建更复杂的图+spfa)
bazel是否有学习的必要
LeetCode_哈希表_困难_149. 直线上最多的点数
终端安全能力验证环境搭建和渗透测试记录
思特奇加入openGauss开源社区,共同推动数据库产业生态发展
selenium 查找b或p标签的内容
JS common method encapsulation
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
Introduction to TS, constructor and its this, inheritance, abstract class and interface
ODPS 下一个map / reduce 准备
How to send samples when applying for BS 476-7 display? Is it the same as the display??