1. 创建一个Student对象:只写name属性和Age属性,重载toString(), 一个空的构造函数;
public class Student{
private String name;
private int 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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student(){}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+","+"age="+age;
}
}
2. 创建一个SortList类:
2.1 方法一:
public class SortList {
public static void printList(List<Student> list){
Iterator<Student> stu = list.listIterator();
while(stu.hasNext()){
System.out.println(stu.next());
}
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("shirley",28));
list.add(new Student("davie",25));
list.add(new Student("yoyo",26));
//排序
Collections.sort(list, new Comparator<Object>(){
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Student && o2 instanceof Student){
Student stu1 = (Student) o1;
Student stu2 = (Student) o2;
return stu1.getAge()-stu2.getAge();
}
return 0;
}
});
//遍历并打印
printList(list);
}
}
2.2 方法二:
2.2.1 改写Student类:
public class Student implements Comparable<Object>{
private String name;
private int 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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student(){}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+","+"age="+age;
}
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student s = (Student) o;
return this.getAge() - s.getAge();
}
return 0;
}
}
2.2.2 排序
public class SortList {
public static void printList(List<Student> list){
Iterator<Student> stu = list.listIterator();
while(stu.hasNext()){
System.out.println(stu.next());
}
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("shirley",28));
list.add(new Student("davie",25));
list.add(new Student("yoyo",26));
//排序
Collections.sort(list);
//遍历并打印
printList(list);
}
}
2.3 方法三
2.3.1 改写Student类
public class Student{
private String name;
private int 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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student(){}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+","+"age="+age;
}
}
// 新建一个比较类
class CompareStu implements Comparator<Object>{
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Student && o2 instanceof Student){
Student stu1 = (Student) o1;
Student stu2 = (Student) o2;
return stu1.getAge() - stu2.getAge();
}
return 0;
}
}
2.3.2 排序
public class SortList {
public static void printList(List<Student> list){
Iterator<Student> stu = list.listIterator();
while(stu.hasNext()){
System.out.println(stu.next());
}
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("shirley",28));
list.add(new Student("davie",25));
list.add(new Student("yoyo",26));
//排序
CompareStu cs = new CompareStu(); //初始化比较类
Collections.sort(list,cs);
//遍历并打印
printList(list);
}
}
3:总结
对于这三种排序方法,个人觉得第一种比较好,主要因为在平时编程的过程中,对象类一般都有其他类在调用,所以不会轻易修改。使用第一种方法就是避免这个,当然这个也是要看项目需求的。