重写Compare方法对集合以指定对象的属性进行排序
2018-03-13 21:52
726 查看
首先我们创建一个学生类举例子
创建一个以ID排序的
创建一个以生日排序的类
测试类
获取学生的集合
打印学生的集合
排序输出结果
import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = 8553487662223781924L; private Integer id; private String name; private String birthday; public Student() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + ", birthday=" + birthday + "}"; } }
创建一个以ID排序的
import com.ucarinc.bean.Student; import java.util.Comparator; public class SortStudentsById implements Comparator { @Override public int compare(Object o1, Object o2) { Student stu1 = (Student)o1; Student stu2 = (Student)o2; // ID从小到大排列 return stu1.getId().compareTo(stu2.getId()); // ID从大到小排列 // return stu2.getId().compareTo(stu1.getId()); } }
创建一个以生日排序的类
import com.ucarinc.bean.Student; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Comparator; import java.util.Date; /** * 按照生日对学生进行排序 */ public class SortStudentsByBirthday implements Comparator { @Override public int compare(Object o1, Object o2) { Student stu1 = (Student) o1; Student stu2 = (Student) o2; DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date birthday1 = sdf.parse(stu1.getBirthday()); Date birthday2 = sdf.parse(stu2.getBirthday()); // 按照生日从早到晚排序 return birthday1.compareTo(birthday2); // 按照生日从晚到早排序 // return birthday2.compareTo(birthday1); } catch (ParseException e) { e.printStackTrace(); } return 0; } }
测试类
public static void main(String[] args) { // 得到学生的集合 List<Student> studentList = getStudentList(); printStudentList("排序前", studentList); System.out.println("------------------------------------"); // 根据ID进行排序 Collections.sort(studentList, new SortStudentsById()); printStudentList("根据ID排序后", studentList); System.out.println("------------------------------------"); // 根据生日进行排序 Collections.sort(studentList, new SortStudentsByBirthday()); printStudentList("根据ID排序后,再根据生日排序", studentList); }
获取学生的集合
private static List<Student> getStudentList() { List<Student> students = new ArrayList<>(); Student stu1 = new Student(); stu1.setId(3); stu1.setBirthday("1973-01-01 00:00:00"); stu1.setName("刘能"); Student stu2 = new Student(); stu2.setId(1); stu2.setBirthday("1973-01-01 12:00:00"); stu2.setName("赵四"); Student stu3 = new Student(); stu3.setId(2); stu3.setBirthday("1999-09-09 09:09:19"); stu3.setName("谢广坤"); students.add(stu1); students.add(stu2); students.add(stu3); return stu 4000 dents; }
打印学生的集合
private static void printStudentList(String type, List<Student> students) { for (Student student : students) { System.out.println(type + student); } }
排序输出结果
排序前Student{id=3, name='刘能, birthday=1973-01-01 00:00:00} 排序前Student{id=1, name='赵四, birthday=1973-01-01 12:00:00} 排序前Student{id=2, name='谢广坤, birthday=1999-09-09 09:09:19} ------------------------------------ 根据ID排序后Student{id=1, name='赵四, birthday=1973-01-01 12:00:00} 根据ID排序后Student{id=2, name='谢广坤, birthday=1999-09-09 09:09:19} 根据ID排序后Student{id=3, name='刘能, birthday=1973-01-01 00:00:00} ------------------------------------ 根据ID排序后,再根据生日排序Student{id=3, name='刘能, birthday=1973-01-01 00:00:00} 根据ID排序后,再根据生日排序Student{id=1, name='赵四, birthday=1973-01-01 12:00:00} 根据ID排序后,再根据生日排序Student{id=2, name='谢广坤, birthday=1999-09-09 09:09:19}
相关文章推荐
- 重写Compare方法对集合以指定对象的属性进行排序
- Java:对集合中的对象进行排序需要实现Interface Comparable接口并实现int compareTo(T o)方法
- 逐步为对象集合构建一个通用的按指定属性排序的方法
- (转)逐步为对象集合构建一个通用的按指定属性排序的方法
- .NET/C#中对自定义对象集合进行自定义排序的方法
- Collections.sort方法(可以根据集合中类的属性的比较对集合内的类进行排序)
- java list按照 对象 指定多个字段属性进行排序
- 如何根据对象的属性,对集合(list / set)中的对象进行排序
- 对JavaScript对象数组按指定属性和排序方向进行排序
- .NET/C#中对自定义对象集合进行自定义排序的方法
- 对象集合的属性排序 Compareable:重写 int compare(p1, p2)
- 如何对List集合中的对象进行按某个属性排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- 对集合中的对象进行排序,用collections.sort 到方法
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- 对象集合根据时间属性进行排序
- Java 中List数据,自定义对象按指定的属性进行排序
- java list按照 对象 指定多个字段属性进行排序
- JAVA中根据集合中对象属性排序的方法