您可以使用基于规则的校对器 https://docs.oracle.com/javase/7/docs/api/java/text/RuleBasedCollator.html定义您自己的规则。
的例子custom rule:
String rules = "< c,C < b,B";
上述规则被解码为大写和小写C
的
都出现在大写和小写之前B
比较的时候
字符串。
String customRules = "<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<X<x<Y<y<Z<z";
RuleBasedCollator myRuleBasedCollator = new RuleBasedCollator(customRules);
Collections.sort(listOfColors,myRuleBasedCollator);
System.out.println(listOfColors);
Output:
[Blue, black, blue, Grey, green, Pink, purple, Red]
Edit:而不是写customRules
您可以手动使用以下代码来生成它。
String a = IntStream.range('a', 'z' + 1).mapToObj(c -> Character.toString((char) c))
.flatMap(ch -> Stream
.of("<", ch.toUpperCase(), "<", ch)).collect(Collectors.joining(""));