如何排序字母数字字符串和更改序列?

时间:2013-08-25 14:15:58

标签: java arrays sorting

如果重复的问题请建议我链接,

我的代码是

 public static void main(String[] args) {
      String name ="h498y948759hrh98A722hjDF94yugerTEr892ur48y";
      char[] arr= name.toCharArray();
      Arrays.sort(arr);
      System.out.println(arr);
}

,结果是

 222444457788888999999ADEFTeghhhhjrrrruuyyy

现在我想按照这种排序的顺序 ADEFT222444457788888999999eghhhhjrrrruyyyy或 eghhhhjrrrruuyyy222444457788888999999ADEFT

所以问题是如何改变这个序列? 如果这是错误的排序方式,那么请告诉我一个。

谢谢

2 个答案:

答案 0 :(得分:4)

您可以创建自己的Comparator类/对象并将其传递给Arrays.sort()。不幸的是,您还需要将元素转换为Character

但是,最通用的方法可能是将每个字符视为String并使用Collator,如下例所示:

    //  Rules separated in 3 parts only for convenience
    String rules1= "< A < B < C < D < E < F < G < H < I < J < K < L < M < N < O < P < Q < R < S < T < U < V < W < X < Y < Z" ;
    String rules2= "< a < b < c < d < e < f < g < h < i < j < k < l < m < n < o < p < q < r < s < t < u < v < w < x < y < z" ;
    String rules3= "< 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9" ;
    RuleBasedCollator collator= new RuleBasedCollator(rules1+rules2+rules3) ;

    String input= "h498y948759hrh98A722hjDF94yugerTEr892ur48y" ;

    //  Bulk of the job done here
    String[] arr= input.split("") ;
    Arrays.sort(arr,1,arr.length,collator);

    //  Join back in a single string for presentation
    StringBuilder sb= new StringBuilder() ;
    for(String e: arr )
        sb.append( e );
    System.out.println(sb);

输出

    ADEFTeghhhhjrrrruuyyy222444457788888999999

仅将整理规则更改为

    String rules1= "< 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9" ;
    String rules2= "< A,a < B,b < C,c < D,d < E,e < F,f < G,g < H,h < I,i < J,j < K,k < L,l < M,m < N,n < O,o < P,p < Q,q < R,r < S,s < T,t < U,u < V,v < W,w < X,x < Y,y < Z,z" ;
    RuleBasedCollator collator= new RuleBasedCollator(rules1+rules2) ;

输出

    222444457788888999999ADEeFghhhhjrrrrTuuyyy

Collator的主要优点是它们允许根据内部规则对多字符串进行排序。实际上,这是他们的主要用例。

非常强大,嗯? (是的,我是加拿大,如果你没有猜到:-))

答案 1 :(得分:3)

我已经写了一些示例来演示Comparator概念。它需要Guava

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.google.common.collect.ComparisonChain;
import com.google.common.primitives.Chars;

public class Test {
    private enum Comparators implements Comparator<Character> {
        UPPER_DIGIT_LOWER {
            @Override
            int compare(char lhs, char rhs) {
                return ComparisonChain.start()
                        .compareTrueFirst(Character.isUpperCase(lhs), Character.isUpperCase(rhs))
                        .compareTrueFirst(Character.isDigit(lhs), Character.isDigit(rhs))
                        .compareTrueFirst(Character.isLowerCase(lhs), Character.isLowerCase(rhs))
                        .compare(lhs, rhs)
                        .result();
            }
        },
        LOWER_DIGIT_UPPER {
            @Override
            int compare(char lhs, char rhs) {
                return ComparisonChain.start()
                        .compareTrueFirst(Character.isLowerCase(lhs), Character.isLowerCase(rhs))
                        .compareTrueFirst(Character.isDigit(lhs), Character.isDigit(rhs))
                        .compareTrueFirst(Character.isUpperCase(lhs), Character.isUpperCase(rhs))
                        .compare(lhs, rhs)
                        .result();
            }
        };

        @Override
        public int compare(Character lhs, Character rhs) {
            return compare(lhs.charValue(), rhs.charValue());
        }

        abstract int compare(char lhs, char rhs);
    }

    private static String sortChars(String str, Comparator<Character> cmp) {
        List<Character> chars = Chars.asList(str.toCharArray());
        Collections.sort(chars, cmp);
        return new String(Chars.toArray(chars));
    }

    public static void main(String[] args) {
        String name = "h498y948759hrh98A722hjDF94yugerTEr892ur48y";
        System.out.println(sortChars(name, Comparators.UPPER_DIGIT_LOWER));
        System.out.println(sortChars(name, Comparators.LOWER_DIGIT_UPPER));
    }
}