当前位置:网站首页>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;
}
边栏推荐
- Determine the best implementation of horizontal and vertical screens
- What are the requirements of UL 2043 test for drive housing in the United States?
- Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)
- Postgres establish connection and delete records
- PVC 塑料片BS 476-6 火焰传播性能测定
- Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
- 概率论机器学习的先验知识(上)
- JS common method encapsulation
- Pytoch practice -- MNIST dataset handwritten digit recognition
- CareerCup它1.8 串移包括问题
猜你喜欢
![[case] Application of element display and hiding -- element mask](/img/6e/6ea484a6e5d547e01dd8820af8e314.png)
[case] Application of element display and hiding -- element mask

浅聊我和一些编程语言的缘分

示波器探头对测量带宽的影响

Reading and writing operations of easyexcel

Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w

R语言【数据管理】

【案例】元素的显示与隐藏的运用--元素遮罩

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

使用WebAssembly在浏览器端操作Excel

Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
随机推荐
Learning notes of SAS programming and data mining business case 19
让开发效率飞速提升的跨端方案
Reading and writing operations of easyexcel
js常用方法封装
启牛2980有没有用?开户安全吗、
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
Aitm2-0002 12s or 60s vertical combustion test
sql系列(基础)-第二章 限制和排序数据
Simple getting started example of Web Service
Using webassembly to operate excel on the browser side
Prior knowledge of machine learning in probability theory (Part 1)
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
Clickhouse copy paste multi line SQL statement error
Test of incombustibility of cement adhesives BS 476-4
Écrire une interface basée sur flask
Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)
Introduction of ArcGIS grid resampling method
CLion配置visual studio(msvc)和JOM多核编译
Write an interface based on flask
木板ISO 5660-1 热量释放速率摸底测试