List集合按某个字段排序

本文转自:https://blog.csdn.net/wangjuan_01/article/details/51351633

package wjtest_01;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
public class ListSort<E> {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
 
		List<Student> list = new ArrayList<Student>();
 
		// 创建3个学生对象,年龄分别是20、19、21,并将他们依次放入List中
		Student s1 = new Student();
		s1.setAge(20);
		s1.setUsable(true);
		Student s2 = new Student();
		s2.setAge(19);
		s2.setUsable(true);
		Student s3 = new Student();
		s3.setAge(21);
		s3.setUsable(false);
		list.add(s1);
		list.add(s2);
		list.add(s3);
 
		System.out.println("排序前:" + list);
		Collections.sort(list, new Comparator<Student>() {
 
			@Override
			public int compare(Student o1, Student o2) {
				// 按照学生的年龄进行升序排列
				if (o1.getAge() > o2.getAge()) {
					return 1;
				}
				if (o1.getAge() == o2.getAge()) {
					return 0;
				}
				return -1;
			}
		});
 
		System.out.println("升序排序后:" + list);
 
		Collections.sort(list, new Comparator<Student>() {
 
			public int compare(Student o1, Student o2) {
 
				// 按照学生的年龄进行降序排列
				if (o1.getAge() > o2.getAge()) {
					return -1;
				}
				if (o1.getAge() == o2.getAge()) {
					return 0;
				}
				return 1;
			}
		});
 
		System.out.println("降序排序后:" + list);
 
		Boolean b1 = true; // 创建Boolean对象1
 
		Boolean b2 = false; // 创建Boolean对象0
 
		Boolean b3 = true; // 创建Boolean对象1
		System.out.println(b1);
 
		int i;
 
		i = b1.compareTo(b2); // b1和b2进行比较  
 
		System.out.println(i);//1-0 
 
		i = b2.compareTo(b1); // b2和b1进行比较
 
		System.out.println(i);//0-1
 
		i = b1.compareTo(b3); // b1和b3进行比较
 
		System.out.println(i);//1-1
	}
}
 
class Student{  
    
    private int age;
    
    private Boolean isUsable;
 
	public int getAge() {  
        return age;  
    }  
  
    public Boolean isUsable() {
		return isUsable;
	}
 
	public void setUsable(Boolean isUsable) {
		this.isUsable = isUsable;
	}
 
	public void setAge(int age) {  
        this.age = age;  
    }  
    @Override  
    public String toString() {  
        return getAge()+"";  
    }  
}

List集合对象根据字段排序

//把需要比较的对象实现Comparable接口实现compareTo方法

public class Address implements Comparable

{
String country;
String city;
String name;

public Address(String country, String city, String name) {
super();
this.country = country;
this.city = city;
this.name = name;
}

public String toString(){
return "\nname:"+this.name+" city:"+this.city+" country:"+this.country;
}

@Override
public int compareTo(Address o) {
  //如果国家不相等,那么直接比较其他字段
  if(!this.country.equals(o.country)){
    return this.country.compareTo(o.country);
  }else if(!this.city.equals(o.city)){
    return this.city.compareTo(o.city);
  }else{
    return this.name.compareTo(o.name);
  }
  }
}

//测试类
public class ComparableTest {
public static void main(String[] args) {
  List

list = new ArrayList
();
  Address a1 = new Address("中国", "湖南", "屌丝1");
  Address a2 = new Address("中国", "湖北", "屌丝2");
  Address a3 = new Address("美国", "纽约", "屌丝3");
  Address a4 = new Address("中国", "湖北", "屌丝4");
  Address a5 = new Address("中国", "湖南", "屌丝5");
  Address a6 = new Address("中国", "广西", "屌丝6");
  list.add(a1);
  list.add(a2);
  list.add(a3);
  list.add(a4);
  list.add(a5);
  list.add(a6);

  System.out.println(list);//排序前
  Collections.sort(list);
  System.out.println(list);//排序后

  }
 }
}

//打印结果

[ name:屌丝1 ncity:湖南 ncountry:中国,

name:屌丝2 ncity:湖北 ncountry:中国,

name:屌丝3 ncity:纽约 ncountry:美国,

name:屌丝4 ncity:湖北 ncountry:中国,

name:屌丝5 ncity:湖南 ncountry:中国,

name:屌丝6 ncity:广西 ncountry:中国]

[ name:屌丝6 ncity:广西 ncountry:中国,

name:屌丝2 ncity:湖北 ncountry:中国,

name:屌丝4 ncity:湖北 ncountry:中国,

name:屌丝1 ncity:湖南 ncountry:中国,

name:屌丝5 ncity:湖南 ncountry:中国,

name:屌丝3 ncity:纽约 ncountry:美国]


🐞标题:List集合按某个字段排序
👽作者:ruige
🐾地址:https://jjdhhc.com/articles/2020/11/07/1604716823559.html
🙏感恩:谢谢您的打赏与支持!中间图片是我的微信公众号,扫码关注哦!
支付宝支付 微信公众号 微信支付