java排序 Collections.sort 两种方法详解

Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式:

 public static <T extends Comparable<? super T>> void sort(List<T> list) {
        list.sort(null);
    }
  public static <T> void sort(List<T> list, Comparator<? super T> c) {
        list.sort(c);
    }

首先使用基本类型(此处使用Integer)来演示第一个方法:

 static Integer[] a = {2,3,1};	
 static List<Integer> list=new ArrayList<Integer>(Arrays.asList(a));	
	public static void main(String[] args) {
		System.out.println("排序前:"+list);
	        System.out.println("读书百遍,其义自见");
	        Collections.sort(list);
	        System.out.println("排序后:"+list);
	}

运行结果如下图所示:

 

可以看到,默认的排序是正序,那么如何实现逆序呢,这就要使用第二种方式了,即通过实现Comparator接口的compare方法来完成自定义排序,代码如下:

 static Integer[] a = { 2, 3, 1 };
	static List<Integer> list = new ArrayList<Integer>(Arrays.asList(a));
	public static void main(String[] args) {
		System.out.println("排序前:" + list);
		System.out.println("读书百遍,其义自见");
		Collections.sort(list, new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				// 返回值为int类型,大于0表示正序,小于0表示逆序
				return o2 - o1;
			}
		});
		System.out.println("排序后:" + list);
	}

运行结果如下图所示:

可以看到,已经实现了逆序的排序了。

接下来看看自定义类的排序:

定义一个Student类:

public class Student {

	    private int sno; //学号
	    private String name;//姓名
	    
		public int getSno() {
			return sno;
		}
		public void setSno(int sno) {
			this.sno = sno;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		public Student(int sno, String name) {
			super();
			this.sno = sno;
			this.name = name;
		}
		
		@Override
		public String toString() {
			return "Student [sno=" + sno + ", name=" + name + "]";
		}   		
}

首先使用同样的方式来使用Collections.sort方法:


此时会报错:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<Student>). The inferred type Student is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

意思是参数类型为List<Student>时,sort方法无法执行,原因是泛型没有继承Comparable接口,这种方式稍后再说,我们先使用sort方法的第二种形式:

根据不同的排序方式即可呈现不同的结果。

1.按照学生学号正序排序

        public static void main(String[] args) {	
			 Student stu1 = new Student(2,"至尊宝");
			 Student stu2 = new Student(3,"凤求凰");
			 Student stu3 = new Student(1,"永耀之星");
			 List<Student> list = new ArrayList<Student>(Arrays.asList(stu1,stu2,stu3));
			 System.out.println("排序前:"+list);
			 System.out.println("读书百遍,其义自见");
			 Collections.sort(list,new Comparator<Student>() {
				public int compare(Student o1, Student o2) {
					//按学号正序排序
					return o1.getSno()-o2.getSno();
				}
			});
			 System.out.println("排序后:"+list);
		}	

运行结果如下图所示:

2.按照学生学号逆序排序

         public static void main(String[] args) {	
			 Student stu1 = new Student(2,"至尊宝");
			 Student stu2 = new Student(3,"凤求凰");
			 Student stu3 = new Student(1,"永耀之星");
			 List<Student> list = new ArrayList<Student>(Arrays.asList(stu1,stu2,stu3));
			 System.out.println("排序前:"+list);
			 System.out.println("读书百遍,其义自见");
			 Collections.sort(list,new Comparator<Student>() {
				public int compare(Student o1, Student o2) {
					//按学号逆序排序
					return o2.getSno()-o1.getSno();
				}
			});
			 System.out.println("排序后:"+list);
		}		

运行结果如下图所示:

3.按照学校姓名正序排序

	public static void main(String[] args) {	
			 Student stu1 = new Student(2,"至尊宝");
			 Student stu2 = new Student(3,"凤求凰");
			 Student stu3 = new Student(1,"永耀之星");
			 List<Student> list = new ArrayList<Student>(Arrays.asList(stu1,stu2,stu3));
			 System.out.println("排序前:"+list);
			 System.out.println("读书百遍,其义自见");
			 Collections.sort(list,new Comparator<Student>() {
				public int compare(Student o1, Student o2) {
					//按学生姓名正序排序
					return o1.getName().compareTo(o2.getName());
				}
			});
			 System.out.println("排序后:"+list);
		}	
运行结果如下图所示:


4.按照学生姓名逆序排序:

	public static void main(String[] args) {	
			 Student stu1 = new Student(2,"至尊宝");
			 Student stu2 = new Student(3,"凤求凰");
			 Student stu3 = new Student(1,"永耀之星");
			 List<Student> list = new ArrayList<Student>(Arrays.asList(stu1,stu2,stu3));
			 System.out.println("排序前:"+list);
			 System.out.println("读书百遍,其义自见");
			 Collections.sort(list,new Comparator<Student>() {
				public int compare(Student o1, Student o2) {
					//按学生姓名逆序排序
					return o2.getName().compareTo(o1.getName());
				}
			});
			 System.out.println("排序后:"+list);
		}	
运行结果如下图所示:



接下来看看第一种形式的实现

首先让Student类继承Comparable接口并重写compareTo方法(为了和上面的排序方式区别开,此次按照学生姓名逆序排列):

public class Student implements Comparable<Student>{
	
    private int sno;
    private String name;
    
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	private Student(int sno, String name) {
		super();
		this.sno = sno;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", sno=" + sno + "]";
	}
	public int compareTo(Student stu) {
		//按学号正序排序
		//return this.getSno()-stu.getSno();
		//按学号逆序排序
		//return stu.getSno()-this.getSno();
		//按学生姓名正序排序
		//return this.getName().compareTo(stu.getName());
		//按学生姓名逆序排序
		return stu.getName().compareTo(this.getName());
	}
	
	public static void main(String[] args) {
		 Student stu1 = new Student(2,"至尊宝");  
	     Student stu2 = new Student(3,"凤求凰");  
	     Student stu3 = new Student(1,"永耀之星");  
	     List<Student> list = new ArrayList<Student>(Arrays.asList(stu1,stu2,stu3));  
		 System.out.println("排序前:"+list);  
	     System.out.println("读书百遍,其义自见");  
	     Collections.sort(list);  
	     System.out.println("排序后:"+list);  
	}   
}
运行结果如下图所示:



总结:

1.对于String或Integer这些已经实现Comparable接口的类来说,可以直接使用Collections.sort方法传入list参数来实现默认方式(正序)排序;

2.如果不想使用默认方式(正序)排序,可以通过Collections.sort传入第二个参数类型为Comparator来自定义排序规则;

3.对于自定义类型(如本例子中的Student),如果想使用Collections.sort的方式一进行排序,可以通过实现Comparable接口的compareTo方法来进行,如果不实现,则参考第2点;

4.jdk1.8的Comparator接口有很多新增方法,其中有个reversed()方法比较实用,是用来切换正序和逆序的,代码如下:

      System.out.println("排序前:"+list);  
	     System.out.println("读书百遍,其义自见");  
	     Comparator<Student> comparator =new Comparator<Student>() {
			public int compare(Student o1, Student o2) {
				//按学号正序排序
				//return o1.getSno()-o2.getSno();
				//按学号逆序排序
				//return o2.getSno()-o1.getSno();
				//按学生姓名正序排序
				//return o1.getName().compareTo(o2.getName());
				//按学生姓名逆序排序
				return o2.getName().compareTo(o1.getName());
			}
		};
		/*新的排序实现方式*/
	     Collections.sort(list,comparator.reversed());  
	     System.out.println("排序后:"+list);  

运行结果如下图所示:

重写的compare方法定义的是按学生姓名逆序排序,在使用reversed翻转后是按学生姓名正序排序,结果如下:


  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值