当前位置:网站首页>Use of natural sorting comparable
Use of natural sorting comparable
2022-06-13 05:05:00 【Jason_ LH1024】

package com.it;
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
// return 1;// positive sequence
// return -1; Descending
// int num = this.age - o.age; // Ascending
int num = o.age-this.age; // Descending
// return num;
// When the age is the same , Than name , In alphabetical order
int num2 = num == 0 ? this.name.compareTo(o.name) : num;
return num2;
}
}
package com.it;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>();
Student s1 = new Student(" Huluwa ", 23);
Student s2 = new Student(" Peter Jackson's King Kong ", 22);
Student s3 = new Student(" monkey ", 24);
Student s4 = new Student(" Dog ",24);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
// Traverse
for (Student s : ts) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}



边栏推荐
- External sort
- C language learning log 11.7
- metaRTC4.0稳定版发布
- Configuration used by automatic teaching evaluation script
- Time display of the 12th Blue Bridge Cup
- CMB's written test -- data analysis
- Listiterator list iterator
- QT signal is automatically associated with the slot
- Four methods for judging JS data types and user-defined methods
- Bm1z002fj-evk-001 startup evaluation
猜你喜欢

Understanding inode

C language learning log 11.7

priority inversion problem

Interpretation of QT keypressevent

Hidden implementation and decoupling, knowing Pimpl mode

Ruoyi cloud startup tutorial (hand-held graphics)

Regular expressions in QT

2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation

Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display

RuoYi-Cloud启动教程(手把手图文)
随机推荐
[multithreading] thread pool core class -threadpoolexecutor
Binary search and binary answer
Sort (internal sort) + external sort
Mysql8.0.13 installation tutorial (with pictures)
Implementing the driver registration initcall mechanism on stm32
1.2 differences caused by keywords
Section 5 - Operator details
Logical point
Regular expressions in QT
Several methods of identifying equivalent circuit of circuit drawing
Clause 34: lambda is preferred over std:: bind
Std:: Map empty example
Force deduction 121 questions
Violence enumeration~
Listiterator list iterator
C language learning log 10.6
Article 49: understanding the behavior of new handler
QT client development -- driver loading problem of connecting to MySQL database
Understanding inode
std::condition_ variable::wait_ for