冒泡排序,排序字符

package com.order.test;


public class Order {

	public static void main(String[] args) {
		
		String [] datas=new String[]{"75","新浪","70","中华人民","90","95","85","80","X","L","XL","XML","12","A","B","D","Z","Y","X","110","150","140","130","99"};
         for (int i = 0; i < datas.length -1; i++){    //最多做n-1趟排序
             for(int j = 0 ;j < datas.length - i - 1; j++){    //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
            	 String value1=datas[j];
            	 String value2=datas[j+1];
            	 if(checkNumber(value1)&&checkNumber(value2)){
            		 if(Integer.parseInt(value1)>Integer.parseInt(value2)){
            			 String temp = datas[j];
            			 datas[j] = datas[j + 1];
            			 datas[j + 1] = temp;
            		 }
            	 }else if(checkNumber(value2)&&!checkNumber(value1)){
            			 String temp = datas[j];
            			 datas[j] = datas[j+1];
            			 datas[j+1] = temp;
            	 }
             }            
         }
             System.out.print("最终排序结果:");
             for(int a = 0; a < datas.length; a++){
                 System.out.print(datas[a] + " ");
        }
	}
	private static boolean checkNumber(String key){
		for(int i=0;i<key.length();i++){
			char ch=key.charAt(i);
			if(ch<‘0‘||ch>‘9‘){
				return false;
			}
		}
		
		return true;
	}
	
	
}

结果:

最终排序结果:12 70 75 80 85 90 95 99 110 130 140 150 新浪 中华人民 X L XL XML A B D Z Y X

冒泡排序,排序字符

上一篇:Linux命令详解之正则表达式


下一篇:fastdfs storage节点制作过程1