当前位置:网站首页>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;
}
边栏推荐
- LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
- 请查收.NET MAUI 的最新学习资源
- Écrire une interface basée sur flask
- 概率论机器学习的先验知识(上)
- 事项研发工作流全面优化|Erda 2.2 版本如“七”而至
- 五层网络协议
- Utils/index TS tool function
- Selenium's method of getting attribute values in DOM
- Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
- SQL series (basic) - Chapter 2 limiting and sorting data
猜你喜欢

2022-07-03-cka- latest feedback from fans

Arcgis\qgis no plug-in loading (no offset) mapbox HD image map

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

Golang(1)|从环境准备到快速上手

2022-07-03-CKA-粉丝反馈最新情况

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

Using webassembly to operate excel on the browser side

张丽俊:穿透不确定性要靠四个“不变”

Introduction of ArcGIS grid resampling method

Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
随机推荐
PVC 塑料片BS 476-6 火焰传播性能测定
Selenium's method of getting attribute values in DOM
Viewrootimpl and windowmanagerservice notes
Utils/index TS tool function
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
Postgres establish connection and delete records
leetcode:1755. Sum of subsequences closest to the target value
Is Kai Niu 2980 useful? Is it safe to open an account
AITM2-0002 12s或60s垂直燃烧试验
selenium 获取dom内属性值的方法
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
hdu2377Bus Pass(构建更复杂的图+spfa)
Reading and writing operations of easyexcel
R language [data management]
MySQL 千万数据量深分页优化, 拒绝线上故障!
Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
思特奇加入openGauss开源社区,共同推动数据库产业生态发展