現(xiàn)有一個(gè)List<User>集合,User中有這兩個(gè)屬性(String name; Integer age) 要通過(guò)age對(duì)list進(jìn)行重新排序,以下有三個(gè)方法:
第一種方法:讓User實(shí)現(xiàn) (implements) Comparable接口重寫(xiě)compareTo方法:
public class User implements Comparable<User>{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(User o) {
return this.age.compareTo(o.getAge()); //默認(rèn)是升序排列,前提是age必須是Integer類(lèi)型
/*if(this.getAge()>o.getAge()) { //這個(gè)也是升序排列
return 1; //代碼含義 :是如果當(dāng)前對(duì)象的數(shù)值與對(duì)比對(duì)象的數(shù)值大的話 返回 1 ,就是說(shuō)(o對(duì)象)放在了前邊
}
if(this.getAge()<o.getAge()) {
return -1; //代碼含義 :是如果當(dāng)前對(duì)象的數(shù)值與對(duì)比對(duì)象的數(shù)值小的話 返回 -1 ,就是說(shuō)(當(dāng)前對(duì)象)放在了前邊
}
return 0;*/ //代碼含義 :return 說(shuō)明這兩個(gè)值相等 不進(jìn)行排序
/*
* 定義降序,代碼如下
* if(this.getAge()>o.getAge()){
* return -1; //代碼含義 :是如果當(dāng)前對(duì)象的數(shù)值與對(duì)比對(duì)象的數(shù)值大的話 返回 -1 ,就是說(shuō)(當(dāng)前對(duì)象)放在了前邊
* }
* if(this.getAge()<o.getAge()){
* return 1; //代碼含義 :是如果當(dāng)前對(duì)象的數(shù)值與對(duì)比對(duì)象的數(shù)值大的話 返回 -1 ,就是說(shuō)(o對(duì)象)放在了前邊
* }
* return 0 ;
* */
}
}
return 1;//大于
return -1;//小于
通過(guò)Collections.sort(userList)重新排序
參考地址:https://blog.csdn.net/wuya112709/article/details/51422156 對(duì)comparable中 1、-1、0 的理解
第二種方法:如果User類(lèi)沒(méi)有實(shí)現(xiàn)comparable接口,可以自定義一個(gè)比較器(comparator<T>),重寫(xiě)compare方法自定義排序規(guī)則;
public class MyComparator implements Comparator<T>{
@Override
public int compare(User o1, User o2) {
if(o1.getAge()>o2.getAge()) {//升序
return 1; //代碼含義:如果第一個(gè)數(shù)值大于第二個(gè)數(shù)值 返回 1,就是說(shuō)把o1放在了o2前
}
if(o1.getAge()<o2.getAge()
return -1;//代碼含義:如果第一個(gè)數(shù)值小于都二分?jǐn)?shù)值 返回 -1,就是說(shuō)把o2放在o1前
}
return 0;
}
}
通過(guò)Collections.sort(userList,new MyComparator())重新排序
降序就是
if(o1.getAge()>o2.getAge()) {//升序
return -1; //代碼含義:如果第一個(gè)數(shù)值大于第二個(gè)數(shù)值 返回 -1,就是說(shuō)把o1放在了o2后
}
if(o1.getAge()<o2.getAge()
return 1;//代碼含義:如果第一個(gè)數(shù)值小于都二分?jǐn)?shù)值 返回 1,就是說(shuō)把o1放在了o2前
}
return 0;
第三種方法:通過(guò)Collections.sort(userList,new comparator<User>(){
重載compare方法,定義排序規(guī)則
})
public static void main(String[] args) {
List<User> uList = new ArrayList<>();
uList.add(new User("夏普",22));
uList.add(new User("二貨",1));
uList.add(new User("燙頭的",14));
uList.add(new User("抽煙的",12));
uList.add(new User("趙本山",24));
uList.add(new User("韓曉娟",3));
uList.add(new User("王海",41));
System.out.println("排序之前:"+uList);
Collections.sort(uList, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
if(o1.getAge()<o2.getAge()) {
return 1;
}
if(o1.getAge()>o2.getAge()) {
return -1;
}
return 0;
}
});
System.out.println("排序之后:"+uList);
}
前兩種方法一定要調(diào)用Collections.sort(userList)方法才會(huì)生效。
童年的紙飛機(jī),什么時(shí)候能飛回我手里
浙公網(wǎng)安備 33010602011771號(hào)