集合的排序

public static void main (String [] args){
		ArrayList <Apple> list = new ArrayList<Apple>();//创建集合,并加入对象
		list.add(new Apple("001",22,30));
		list.add(new Apple("002",22,40));
		list.add(new Apple("003",2,3));
		list.add(new Apple("004",10,10));
		Collections.sort(list,new Comparator<Apple>(){ //重写sort里的方法

			@Override
			public int compare(Apple o1, Apple o2) {
				if(o1.zhong-o2.zhong>0){
					return 1;
				}else if (o1.zhong-o2.zhong==0){
					if(o1.tiji-o2.tiji>=0){
						return 1;
					}else{
						return -1;
					}
				}else{
					return -1;
				}
			}
		});
	 	for(Apple app:list){
	 		System.out.println(app);
	 	}
	    Collections.sort(list,new Comparator<Apple>(){
//重写sort里的方法
@Override public int compare(Apple o1, Apple o2) { if(o2.zhong-o1.zhong>0){ return 1; }else if (o2.zhong-o1.zhong==0){ if(o2.tiji-o1.tiji>=0){ return 1; }else{ return -1; } }else{ return -1; } } }); System.out.println("----------------------------------------------------"); for(Apple app:list){ System.out.println(app); } } }

  

posted @ 2017-08-08 09:19  fy02223y  阅读(200)  评论(0编辑  收藏  举报