java自定义排序

java自定义排序~~~

package com.catalogeasy.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author vanilla  
 * @since 2009年5月21日21:05:30
 */



public class Sort {

	private String[] _s;
	 ArrayList<SStruct> SList=new ArrayList<SStruct>();  
	
	public Sort(String[] s) {
		// TODO Auto-generated constructor stub
		int i ;
		for(i=0;i<s.length;i++){
		 SStruct sStruct =new SStruct(s[i]);  
		 SList.add(sStruct); 
		}
		 
		    
		 SCompare sCompare=new SCompare();  
		     Collections.sort(SList,sCompare);  
		     
		     i = 0;
		     for(Iterator it = SList.iterator(); it.hasNext(); ) {
		    	SStruct o = (SStruct)it.next();
		    	s[i++] = o.str;
		     }
		    
		    	
		
		this._s = s;
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i;
		String[] s = new String[40];
		for(i=0;i<20;i++){
			s[i] = String.valueOf(i);
		}
		for(i=0;i<10;i++){
			s[i+20] = String.valueOf(i+10) + "a";
		}
		for(i=0;i<10;i++){
			s[i+30] = String.valueOf(i+1) + "ab";
		}
		Sort o = new Sort(s);
		for(i=0;i<s.length;i++){
			
			System.out.println(s[i]);
			
		}
		
		

	}
	public String[] get_s() {
		return _s;
	}
}
class SStruct{
	public  SStruct(String s){
		this.str = s;
		Pattern p = Pattern.compile("[0-9]");
		Matcher m = p.matcher(s);
		this.pStr = m.replaceAll("");//剩下字符串
		p = Pattern.compile("[a-zA-Z]");
	    m = p.matcher(s);
		this.pNum = Integer.valueOf(m.replaceAll(""));//剩下num		
	}

	public String str;
	public int pNum;
	public String pStr;
}
class SCompare implements Comparator<SStruct>  
{  
    public int compare(SStruct one,SStruct two){  
    	if(one.pNum == two.pNum){
    		return one.pStr.compareTo(two.pStr);  
    	}else{
    		if(one.pNum > two.pNum)return 1;	        		
    		else return -1;
    	}
    }  
}  

你可能感兴趣的:(java)