冒泡排序,排序字符

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章