java多条件优先级排序 --------- Comparator

Comparator用法代码 复制代码  收藏代码
  1. package com;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collections;   
  5. import java.util.Comparator;   
  6. import java.util.List;   
  7.   
  8. public class ComparatorDemo {   
  9.     public List<Student> mList;   
  10.     public List<Comparator<Student>> mCmpList = new ArrayList<Comparator<Student>>();   
  11.     public ComparatorDemo(List<Student> list){   
  12.         mList = list;   
  13.         mCmpList.add(compareAgeASC);   
  14.         mCmpList.add(comparePointDESC);   
  15.         sort(mList, mCmpList);   
  16.     }   
  17.     public void sort(List<Student> list, final List<Comparator<Student>> comList) {   
  18.         if (comList == null)   
  19.             return;   
  20.         Comparator<Student> cmp = new Comparator<Student>() {   
  21.             @Override   
  22.             public int compare(Student o1, Student o2) {   
  23.                 for (Comparator<Student> comparator : comList) {   
  24.                     if (comparator.compare(o1, o2) > 0) {   
  25.                         return 1;   
  26.                     } else if (comparator.compare(o1, o2) < 0) {   
  27.                         return -1;   
  28.                     }   
  29.                 }   
  30.                 return 0;   
  31.             }   
  32.         };   
  33.         Collections.sort(list, cmp);   
  34.     }   
  35.   
  36.     private Comparator<Student> compareAgeASC = new Comparator<ComparatorDemo.Student>() {   
  37.   
  38.         @Override   
  39.         public int compare(Student o1, Student o2) {   
  40.             return o1.age > o2.age ? 1 : -1;   
  41.         }   
  42.     };   
  43.   
  44.     private Comparator<Student> comparePointDESC = new Comparator<ComparatorDemo.Student>() {   
  45.   
  46.         @Override   
  47.         public int compare(Student o1, Student o2) {   
  48.             return o1.point < o2.point ? 1 : -1;   
  49.         }   
  50.     };   
  51.   
  52.     /**   
  53.      * @author 80059130  
  54.      *    
  55.      */   
  56.     class Student {   
  57.         public int age;   
  58.         public String name;   
  59.         public int point;   
  60.     }   
  61. }  
package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorDemo {
	public List<Student> mList;
	public List<Comparator<Student>> mCmpList = new ArrayList<Comparator<Student>>();
	public ComparatorDemo(List<Student> list){
		mList = list;
		mCmpList.add(compareAgeASC);
		mCmpList.add(comparePointDESC);
		sort(mList, mCmpList);
	}
	public void sort(List<Student> list, final List<Comparator<Student>> comList) {
		if (comList == null)
			return;
		Comparator<Student> cmp = new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				for (Comparator<Student> comparator : comList) {
					if (comparator.compare(o1, o2) > 0) {
						return 1;
					} else if (comparator.compare(o1, o2) < 0) {
						return -1;
					}
				}
				return 0;
			}
		};
		Collections.sort(list, cmp);
	}

	private Comparator<Student> compareAgeASC = new Comparator<ComparatorDemo.Student>() {

		@Override
		public int compare(Student o1, Student o2) {
			return o1.age > o2.age ? 1 : -1;
		}
	};

	private Comparator<Student> comparePointDESC = new Comparator<ComparatorDemo.Student>() {

		@Override
		public int compare(Student o1, Student o2) {
			return o1.point < o2.point ? 1 : -1;
		}
	};

	/**
	 * @author 80059130
	 * 
	 */
	class Student {
		public int age;
		public String name;
		public int point;
	}
}

  1、Comparator

  Collections.sort(List<T> list, Comparator<? super T> c) ;

  2、T实现了Comparable 接口

  Collections.sort(List<T> list);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值