利用sout方法排序

Posted 王。雄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用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);
    }
	}
	
}

  

以上是关于利用sout方法排序的主要内容,如果未能解决你的问题,请参考以下文章

如何在Sublime Text中添加代码片段

22.java方法的定义

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

java idea常用快捷键

java idea常用快捷键