对数字(或者中文+数字)进行排序,非数组排序

**对数字(或者中文+数字)进行排序,非数组排序

“`
package zza_java_tesst4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WindowComparator1 implements Comparable{

String name = null;
public WindowComparator1(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 WindowComparator1){
        WindowComparator1 other = (WindowComparator1)obj;
        if(null == this.name){
            return false;
        } else {
            return this.name.equals(other.name);
        }
    }
    return false;
}
// 比较方法,相当于减法。 (return this - wrapper)
public int compareTo(WindowComparator1 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<WindowComparator1> chinesesOrderList = new ArrayList<WindowComparator1>();  

// chinesesOrderList.add(new WindowComparator1(“我们80后相亲奇遇记-1.mp3”));
// chinesesOrderList.add(new WindowComparator1(“他80后相亲奇遇记-10.mp3”));
// chinesesOrderList.add(new WindowComparator1(“啊80后相亲奇遇记-12.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-11.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-13.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-25.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-26.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-2.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-3.mp3”));
chinesesOrderList.add(new WindowComparator1(“我80后相亲奇遇记-4.mp3”));
// chinesesOrderList.add(new WindowComparator1(“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++) {  
        WindowComparator1 chinese = chinesesOrderList.get(i);  
        System.out.println("" + chinese);  
    }  
}

public static void main(String[] args) {
    testNew();
}

}
“`**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值