萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> Java字符串排序中文+數字

Java字符串排序中文+數字

  思路: 在Java中,排序需要復寫的是 equals 方法 和 Comparable<T> 接口 的public int compareTo(T o); 方法

步驟:

1. 使用正則表達式來判斷數字,多個連續的數字作為一組,

2. 一次檢索出數字組合,

3. 檢出下一組數字,如果有,則進入步驟4,否則進入步驟6.

4. 如果兩組數字出現的位置相等,並且前面部分的字符串相等,則進入第5步。否則break,跳到第6步.

5. 如果前面部分的字符串完全一致。則比較兩個數字的大小,如果大小一致,則進入下一組,即步驟3.如果大小不一致,則可以比對出來大小,比較結束

6. 調用String的compareTo方法,病返回(流程結束)。
完整的代碼如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//包裝器類
public class OrderWrapper implements Comparable<OrderWrapper>{
	String name = null;
	public OrderWrapper(String name){
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return String.valueOf(name);
	}
	@Override
	public boolean equals(Object obj) {
		if(obj == this){
			return true;
		} 
		
		if(obj instanceof OrderWrapper){
			OrderWrapper other = (OrderWrapper)obj;
	
			if(null == this.name){
				return false;
			} else {
				return this.name.equals(other.name);
			}
		}
		return false;
	}

	// 比較方法,相當於減法。 (return this - wrapper)
	public int compareTo(OrderWrapper wrapper) {
		if(null == wrapper){
			return 1;
		}
		// 直接相等
		if(this == wrapper || this.equals(wrapper)){
			return 0;
		}
		String name1 = this.name;
		String name2 = wrapper.name;
		// 特殊情形,name有一個為空的情況.
		if(null == name1){
			// 都為空,認為相對
			if(null == name2){
				return 0;
			} else {
				return -1;
			}
		} else if(null == name2){
			return 1;
		}
		// 中間 1-多個數字
		Pattern pattern = Pattern.compile("D*(d+)D*");
		Matcher matcher1 = pattern.matcher(name1);
		Matcher matcher2 = pattern.matcher(name2);
		//System.out.println(pattern.pattern());
		//
		int index1_step = 0;
		int index2_step = 0;
		while(matcher1.find()){
			String s1 = matcher1.group(1);
			String s2 = null;
			if(matcher2.find()){
				s2 = matcher2.group(1);
			}
			int index1 = name1.indexOf(s1, index1_step);
			int index2 = name2.indexOf(s2, index2_step);
			//
			index1_step = index1;
			index2_step = index2;
			// 索引相等的情況下
			if(index1 == index2){
				System.out.println("name1="+name1.length()+"nname2="+name2.length());
				System.out.println("index1="+index1+",index2="+index2);
				String pre1 = name1.substring(0, index1);
				String pre2 = name2.substring(0, index2);
				if(pre1.equals(pre2)){
					// 
					long num1 = Long.parseLong(s1);
					long num2 = Long.parseLong(s2);
					//
					if(num1 == num2){
						// 比較下一組
						continue;
					} else {
						return (int)(num1 - num2);
					}
				} else {
					break;
				}
			} else {
				break;
			}
		}
		
		// 最後的情形.
		return this.name.compareTo(wrapper.name);
	}
	

	public static void testNew(){
		List<OrderWrapper> chinesesOrderList = new ArrayList<OrderWrapper>();  
        chinesesOrderList.add(new OrderWrapper("我們80後相親奇遇記-1.mp3"));  
        chinesesOrderList.add(new OrderWrapper("他80後相親奇遇記-10.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-11.mp3"));  
        chinesesOrderList.add(new OrderWrapper("啊80後相親奇遇記-12.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-13.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-25.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-26.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-2.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-3.mp3"));  
        chinesesOrderList.add(new OrderWrapper("我80後相親奇遇記-4.mp3"));  
        chinesesOrderList.add(new OrderWrapper("a80後相親奇遇記-4.mp3"));  
   
        //Collator collatorChinese = Collator.getInstance(java.util.Locale.CHINA);  
        //collatorChinese = Collator.getInstance(java.util.Locale.CHINESE);  
       // Collections.sort(chinesesOrderList, collatorChinese);  
        Collections.sort(chinesesOrderList);  

        System.out.println("中文+數字排序: = ");  
        for (int i = 0; i < chinesesOrderList.size(); i++) {  
        	OrderWrapper chinese = chinesesOrderList.get(i);  
            System.out.println("" + chinese);  
        }  

	}
	
	public static void main(String[] args) {
		testNew();
	}
}
copyright © 萬盛學電腦網 all rights reserved