1. 程式人生 > >Comparator如何進行排序的

Comparator如何進行排序的

Comparator c = new Comparator<Department>() {
			@Override
			public int compare(Department d1,Department d2){
				List<School> list1 = schoolDao.findById(d1.getSchool_id());
				List<School> list2 = schoolDao.findById(d2.getSchool_id());
				School s1 = list1.get(0);
				School s2 = list2.get(0);
				
				if(s1.getAhp()<=s2.getAhp()){
					return 1; //按照ahp值降序排列
				}
				else{
					return -1;
				}
			}
		};

若compare返回值大於0,則升序

若compare返回值小於0,則降序