Collections.sort()方法为List集合内对象进行排序

此处算是做一个笔记吧,我怕忘了。

对于List排序我们用的还是不少,我们平常都是用的Collections.sort()的方法,对于我们所需要排序的List内的对象是有要求的。那就是必须要实现Comparable这个接口,重写compare()这个方法。因此这种情况下我们一般会有两种方法来进行排序。我这儿用的是一个Student类,有String类型的name字段,int类型的year字段,String类型的day字段。这儿解释一下day字段,由于这儿的比较需要,我是用的“月-日”来表示的,此地需要才这样写,一般情况下不建议。

第一种方法:

Student类:

package com.uitrs.game.reg.common;

/**
 * @author 作者 BealHe
 * @date 创建时间:2019年4月22日 下午2:44:06
 * @explain 类说明 :学生类
 */
public class Student {
	/* 姓名 */
	private String name;
	/* 出生年 */
	private int year;
	/* 出生月-日(此地需要才这样写,一般情况下不建议) */
	private String day;

	public Student() {
		super();
	}

	public Student(String name, int year, String day) {
		super();
		this.name = name;
		this.year = year;
		this.day = day;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", year=" + year + ", day=" + day + "]";
	}
}

下面是主方法和排序方法:

public static void main(String[] args) throws Exception {
		List list = new ArrayList<>();
		Student s1 = new Student("张三", 1992, "2-5");
		list.add(s1);
		s1 = new Student("李四", 1992, "12-5");
		list.add(s1);
		s1 = new Student("王五", 1992, "2-20");
		list.add(s1);
		s1 = new Student("万一", 1992, "12-20");
		list.add(s1);
		s1 = new Student("开心", 1993, "3-12");
		list.add(s1);
		s1 = new Student("幸福", 1993, "3-5");
		list.add(s1);
		s1 = new Student("刘二", 1990, "2-5");
		list.add(s1);
		s1 = new Student("书桌", 1991, "2-5");
		list.add(s1);

		for (Student student : list) {
			System.out.println(student.toString());
		}
		System.out.println("----------------------分割线----------------------");
		sort(list);
		for (Student student : list) {
			System.out.println(student.toString());
		}
	}

	/**
	 * 学生年龄大小比较,首先比较年份,其次比较月份和日期
	 * 
	 * @param list
	 */
	private static void sort(List list) {
		Collections.sort(list, new Comparator() {
			@Override
			public int compare(Student o1, Student o2) {
				Integer year1 = o1.getYear();
				Integer year2 = o2.getYear();
				// 比较年份
				if (0 == year1.compareTo(year2)) {
					String[] arr1 = o1.getDay().split("-");
					String[] arr2 = o2.getDay().split("-");
					Integer month1 = Integer.parseInt(arr1[0]);
					Integer month2 = Integer.parseInt(arr2[0]);
					// 比较月份
					if (0 == month1.compareTo(month2)) {
						Integer day1 = Integer.parseInt(arr1[1]);
						Integer day2 = Integer.parseInt(arr2[1]);
						// 比较日期
						return day1.compareTo(day2);
					} else {
						return month1.compareTo(month2);
					}
				} else {
					return year1.compareTo(year2);
				}
			}
		});
	}

然后是排序前后输出结果对照:

Student [name=张三, year=1992, day=2-5]
Student [name=李四, year=1992, day=12-5]
Student [name=王五, year=1992, day=2-20]
Student [name=万一, year=1992, day=12-20]
Student [name=开心, year=1993, day=3-12]
Student [name=幸福, year=1993, day=3-5]
Student [name=刘二, year=1990, day=2-5]
Student [name=书桌, year=1991, day=2-5]
----------------------分割线----------------------
Student [name=刘二, year=1990, day=2-5]
Student [name=书桌, year=1991, day=2-5]
Student [name=张三, year=1992, day=2-5]
Student [name=王五, year=1992, day=2-20]
Student [name=李四, year=1992, day=12-5]
Student [name=万一, year=1992, day=12-20]
Student [name=幸福, year=1993, day=3-5]
Student [name=开心, year=1993, day=3-12]

上面就是第一种排序的方法,从结果可以看到,我们先以年为排序依据,其次再以月为依据,最后才以日为依据,排序结果没问题。接下来是第二种排序方法。

第二种方法:

实现Comparable的Student类:

package com.uitrs.game.reg.common;

/**
 * @author 作者 BealHe
 * @date 创建时间:2019年4月22日 下午2:44:06
 * @explain 类说明 :学生类
 */
public class Student implements Comparable {
	/* 姓名 */
	private String name;
	/* 出生年 */
	private int year;
	/* 出生月-日(此地需要才这样写,一般情况下不建议) */
	private String day;

	public Student() {
		super();
	}

	public Student(String name, int year, String day) {
		super();
		this.name = name;
		this.year = year;
		this.day = day;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", year=" + year + ", day=" + day + "]";
	}

	@Override
	public int compareTo(Student o) {
		Integer year1 = this.getYear();
		Integer year2 = o.getYear();
		// 比较年份
		if (0 == year1.compareTo(year2)) {
			String[] arr1 = this.getDay().split("-");
			String[] arr2 = o.getDay().split("-");
			Integer month1 = Integer.parseInt(arr1[0]);
			Integer month2 = Integer.parseInt(arr2[0]);
			// 比较月份
			if (0 == month1.compareTo(month2)) {
				Integer day1 = Integer.parseInt(arr1[1]);
				Integer day2 = Integer.parseInt(arr2[1]);
				// 比较日期
				return day1.compareTo(day2);
			} else {
				return month1.compareTo(month2);
			}
		} else {
			return year1.compareTo(year2);
		}
	}
}

下面是排序主方法:

public static void main(String[] args) throws Exception {
		List list = new ArrayList<>();
		Student s1 = new Student("张三", 1992, "2-5");
		list.add(s1);
		s1 = new Student("李四", 1992, "12-5");
		list.add(s1);
		s1 = new Student("王五", 1992, "2-20");
		list.add(s1);
		s1 = new Student("万一", 1992, "12-20");
		list.add(s1);
		s1 = new Student("开心", 1993, "3-12");
		list.add(s1);
		s1 = new Student("幸福", 1993, "3-5");
		list.add(s1);
		s1 = new Student("刘二", 1990, "2-5");
		list.add(s1);
		s1 = new Student("书桌", 1991, "2-5");
		list.add(s1);

		for (Student student : list) {
			System.out.println(student.toString());
		}
		System.out.println("----------------------分割线----------------------");
		Collections.sort(list);
		for (Student student : list) {
			System.out.println(student.toString());
		}
	}

最后是排序前后输出结果对照:

Student [name=张三, year=1992, day=2-5]
Student [name=李四, year=1992, day=12-5]
Student [name=王五, year=1992, day=2-20]
Student [name=万一, year=1992, day=12-20]
Student [name=开心, year=1993, day=3-12]
Student [name=幸福, year=1993, day=3-5]
Student [name=刘二, year=1990, day=2-5]
Student [name=书桌, year=1991, day=2-5]
----------------------分割线----------------------
Student [name=刘二, year=1990, day=2-5]
Student [name=书桌, year=1991, day=2-5]
Student [name=张三, year=1992, day=2-5]
Student [name=王五, year=1992, day=2-20]
Student [name=李四, year=1992, day=12-5]
Student [name=万一, year=1992, day=12-20]
Student [name=幸福, year=1993, day=3-5]
Student [name=开心, year=1993, day=3-12]

上面就是第二种排序的方法,从结果可以看到,和第一次的排序结果一致。

这就是我的两种排序方法,实现方法不同,一个new的Comparator类,一个实现Comparable类并重写compareTo()方法。

你可能感兴趣的:(Collections.sort()方法为List集合内对象进行排序)