2

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

我的代码是

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

结果是

 222444457788888999999ADEFTeghhhhjrrrruuyyy

现在我想改变这种排序的顺序,比如 ADEFT222444457788888999999eghhhhjrrrruuyyy 或 eghhhhjrrrruuyyy222444457788888999999ADEFT

所以问题是我怎样才能改变这个顺序?如果这是错误的排序方式,请告诉我正确的方式。

谢谢

4

2 回答 2

4

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

但是,也许最通用的方法是将每个字符视为 aString并使用 a 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

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

很强大吧?(是的,我是加拿大人,以防你没猜到:-))

于 2013-08-25T14:18:49.333 回答
3

我写了一些示例来演示这个Comparator概念。它需要番石榴

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));
    }
}
于 2013-08-25T14:30:48.803 回答