利用sout方法排序

package Java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;
public class Paixu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	Apple12 a = new Apple12(1,"苹果",3.1,5);	
	Apple12 b = new Apple12(2,"苹果",3.4,3);
	Apple12 c = new Apple12(3,"苹果",3.6,4);
	Apple12 d = new Apple12(3,"苹果",3.4,7);
	Apple12 e = new Apple12(3,"苹果",3.2,2);
	List<Apple12> list =   new ArrayList<Apple12>();
	list.add(a);
	list.add(b);
	list.add(c);
	list.add(d);
	list.add(e);
	Collections.sort(list,new Comparator<Apple12>() {		
		@Override
		public int compare(Apple12 o1, Apple12 o2) {
			// TODO Auto-generated method stub
			
			String str1 = String.valueOf(o1.getC());
			String str2 = String.valueOf(o2.getC());
			int endIndex = str1.length();
			int index11 = str1.indexOf(".");
			String str11 = str1.substring(0, index11);
			String str12 = str1.substring(index11+1, endIndex);
			int c = Integer.parseInt(str11);
			int d = Integer.parseInt(str12);
			
			int endIndex1 = str2.length();
			int index21 = str2.indexOf(".");
			String str21 = str2.substring(0, index21);
			String str22 = str2.substring(index11+1, endIndex1);
			int c1 = Integer.parseInt(str21);
			int d1 = Integer.parseInt(str22);
			if(o1.getD()!=o2.getD()){
			if(c==c1){
				if(d-d1!=0){
					return d-d1;
				
				}else{
					
				}
			}else{
				return (int) (o1.getC()-o2.getC())*(-1);
			}
			}else{
				return (int) (o1.getD()-o2.getD())*(-1);
			}
			return 0;
		}
	});
	
    for(Apple12 x:list){
    	System.out.println("正序x:"+x);
    }
    System.out.println("----------------------------------");
    Collections.sort(list,new Comparator<Apple12>() {		
		@Override
		public int compare(Apple12 o1, Apple12 o2) {
			// TODO Auto-generated method stub
			
			String str1 = String.valueOf(o1.getC());
			String str2 = String.valueOf(o2.getC());
			int endIndex = str1.length();
			int index11 = str1.indexOf(".");
			String str11 = str1.substring(0, index11);
			String str12 = str1.substring(index11+1, endIndex);
			int c = Integer.parseInt(str11);
			int d = Integer.parseInt(str12);
			
			int endIndex1 = str2.length();
			int index21 = str2.indexOf(".");
			String str21 = str2.substring(0, index21);
			String str22 = str2.substring(index11+1, endIndex1);
			int c1 = Integer.parseInt(str21);
			int d1 = Integer.parseInt(str22);
			if(o1.getC()!=o2.getC()){
			if(c==c1){
				if(d-d1!=0){
					return (d-d1)*(-1);
				}
			}else{
				return (int) (o1.getC()-o2.getC());
			}
			}else{
				return (int) (o1.getD()-o2.getD())*(-1);
			}
			return 0;
		}		
	});
	
    for(Apple12 x:list){
    	System.out.println("倒序x:"+x);
    }
	}
	
}

  

posted @ 2017-08-08 09:07  王。雄  阅读(751)  评论(0编辑  收藏  举报