java-list排序,反转

         对数组排序,可以用Arrays.sort()方法,直接用即可,对list排序要麻烦一些。、

          首先,定义一个student对象:

package sort;

/**
 * @ClassName: Student
 * @Description: 这里用一句话描述这个类的作用
 * @author xuejupo [email protected]
 * @date 2015-11-12 上午6:33:03
 */

public class Student  implements Comparable{
	
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	private String name;
	private int age = 0;

	public final String getName() {
		return name;
	}

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

	public final int getAge() {
		return age;
	}

	public final void setAge(int age) {
		this.age = age;
	}
	public String toString(){
		return "name:"+name+"\t"+"age:"+age+"\r\n";
	}

	@Override
	public int compareTo(Object o) {
		if(!(o instanceof Student)){
			return 0;
		}
		return this.age - ((Student)o).getAge();
	}
}

 对list排序有两种方法,一种是list里的object实现implements Comparable接口,并且重载了compareTo方法,那就可以直接调用Coolections.sort()方法了。

   主函数:  实例化20个学生对象,随机生成他们的年龄,放到list里并依照年龄对他们进行排序:

List<Student> l = new ArrayList<Student>();
		for(int i = 0; i < 20; i++){
			Student s = new Student("学生"+i,new Random().nextInt(40));
			l.add(s);
		}
		Collections.sort(l);
		System.out.println(l);

 结果:

[name:学生10	age:7
, name:学生11	age:8
, name:学生13	age:9
, name:学生3	age:12
, name:学生4	age:15
, name:学生17	age:15
, name:学生19	age:17
, name:学生8	age:23
, name:学生9	age:23
, name:学生0	age:25
, name:学生2	age:26
, name:学生5	age:26
, name:学生6	age:26
, name:学生18	age:26
, name:学生1	age:28
, name:学生15	age:28
, name:学生7	age:30
, name:学生14	age:32
, name:学生12	age:38
, name:学生16	age:39
]

 另一种方法是调用Collections.sort(List l,Comparator<> c)方法,这里的c可以称为比较器,可以用匿名内部类,也可以自己写一个类,实现Comparator接口。一般来说,如果这个比较方法经常用到,最好自己写一个类,如果只是临时用到,可以用匿名类。

比如下面,自己写一个类完成对list中student对象的排序,但是后来发现需要将list调转,那就用匿名内部类进行排序即可:

class compare implements Comparator<Student>{

		@Override
		public int compare(Student o1, Student o2) {
			// TODO Auto-generated method stub
			return o1.getAge() - o2.getAge();
		}
		
	}

 主函数:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Student> l = new ArrayList<Student>();
		for(int i = 0; i < 20; i++){
			Student s = new Student("学生"+i,new Random().nextInt(40));
			l.add(s);
		}
		compare c = new compare();
		Collections.sort(l,c);
		System.out.println(l);
		
		/**
		 * 下面利用java自带排序算法对list进行逆转
		 */
		Collections.sort(l,new Comparator<Student>(){
			@Override
			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				return 1;
			}
			
		});
		System.out.println("逆转后:");
		System.out.println(l);
	}

 结果:

[name:学生1	age:1
, name:学生12	age:1
, name:学生14	age:6
, name:学生5	age:7
, name:学生16	age:7
, name:学生6	age:8
, name:学生11	age:8
, name:学生15	age:10
, name:学生3	age:13
, name:学生2	age:14
, name:学生10	age:20
, name:学生4	age:22
, name:学生9	age:23
, name:学生19	age:23
, name:学生13	age:32
, name:学生0	age:34
, name:学生17	age:36
, name:学生18	age:37
, name:学生7	age:38
, name:学生8	age:39
]
逆转后:
[name:学生8	age:39
, name:学生7	age:38
, name:学生18	age:37
, name:学生17	age:36
, name:学生0	age:34
, name:学生13	age:32
, name:学生19	age:23
, name:学生9	age:23
, name:学生4	age:22
, name:学生10	age:20
, name:学生2	age:14
, name:学生3	age:13
, name:学生15	age:10
, name:学生11	age:8
, name:学生6	age:8
, name:学生16	age:7
, name:学生5	age:7
, name:学生14	age:6
, name:学生12	age:1
, name:学生1	age:1
]

 PS:当调用Collections.sort(List l,Comparator<> c)方法的时候,list里面对象自带的compareTo方法(如student类内实现的compareTo方法)是无效的,不会被调用。

 

你可能感兴趣的:(java-list排序,反转)