- // 数字和字母
- class sortNumAndChar implements Comparator {
- public int compare(Object o1, Object o2) {
- // TODO Auto-generated method stub
- String ostr1 = "";
- String ostr2 = "";
- return ostr1.compareToIgnoreCase(ostr2);
- }
- }
- // 中文按拼音排序
- class sortCN implements Comparator {
- public int compare(Object o1, Object o2) {
- // TODO Auto-generated method stub
- String ostr1 = "";
- String ostr2 = "";
- for (int i = 0; i < ostr1.length() && i < ostr2.length(); i++) {
- int codePoint1 = ostr1.charAt(i);
- int codePoint2 = ostr2.charAt(i);
- if (Character.isSupplementaryCodePoint(codePoint1)
- || Character.isSupplementaryCodePoint(codePoint2)) {
- i++;
- }
- if (codePoint1 != codePoint2) {
- if (Character.isSupplementaryCodePoint(codePoint1)
- || Character.isSupplementaryCodePoint(codePoint2)) {
- return codePoint1 - codePoint2;
- }
- String pinyin1 = pinyin((char) codePoint1);
- String pinyin2 = pinyin((char) codePoint2);
- if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字
- if (!pinyin1.equals(pinyin2)) {
- return pinyin1.compareTo(pinyin2);
- }
- } else {
- return codePoint1 - codePoint2;
- }
- }
- }
- return ostr1.length() - ostr2.length();
- }
- private String pinyin(char c) {
- String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
- if (pinyins == null) {
- return null;
- }
- return pinyins[0];
- }
- }
中文拼音简单排序
最新推荐文章于 2023-02-13 11:47:14 发布