Java 中文字符的排序

来源:互联网 时间:1970-01-01

项目中往往需要集合按照中文字符排序,而且java 自带的String 本身

public int compareTo(String anotherString) {int len1 = count;int len2 = anotherString.count;int n = Math.min(len1, len2);char v1[] = value;char v2[] = anotherString.value;int i = offset;int j = anotherString.offset;if (i == j) { int k = i; int lim = n + i; while (k < lim) {char c1 = v1[k];char c2 = v2[k];if (c1 != c2) { return c1 - c2;}k++; }} else { while (n-- != 0) {char c1 = v1[i++];char c2 = v2[j++];if (c1 != c2) { return c1 - c2;} }}return len1 - len2; }


这样并不能满足中文字符的排序,但是你可以使用
Collator java.text.Collator.getInstance()Gets the Collator for the current default locale. The default locale is determined by java.util.Locale.getDefault.
来辅助排序



相关阅读:
Top