隨筆-16  評論-50  文章-2  trackbacks-0

          摘要:在很多中文書籍的作者頁中,有很多作者的話,很多時候,是按作者姓名的筆畫順序排序的。Microsoft的Excel和Sql Server實現了按筆畫排序的功能。那按筆畫順序排序,在Java中究竟怎樣實現呢?


          作者:Jeff 發表于:2007-12-20 20:39 最后更新于: 2007年12月21日 12:17
          版權聲明:可以任意轉載,轉載時請務必以超鏈接形式標明文章原始出處和作者信息及本版權聲明。
          http://www.aygfsteel.com/jeff-lau/archive/2007/12/20/169147.html


          合并了

          已經將《中文排序--筆畫》和《中文排序--漢語拼音》修改并合并《中文排序》中。這里只保留最后的代碼。實現的原理和方法請見中文排序

          /**
            * @author Jeff
            *
            * Copyright (c) 復制或轉載本文,請保留該注釋。
            */
          package chinese.utility;

          import java.util.Comparator;

          public class StrokeComparator implements Comparator<String> {

              public int compare(String o1, String o2) {

                  Chinese chinese = new Chinese();

                  for (int i = 0; i < o1.length() && i < o2.length(); i++) {
                      int codePoint1 = o1.codePointAt(i);
                      int codePoint2 = o2.codePointAt(i);
                      if (codePoint1 == codePoint2)
                          continue;

                      int stroke1 = chinese.stroke(codePoint1);
                      int stroke2 = chinese.stroke(codePoint2);

                      if (stroke1 < 0 || stroke2 < 0) {
                          return codePoint1 - codePoint2;
                      }

                      if (stroke1 != stroke2) {
                          return stroke1 - stroke2;
                      }
                  }

                  return o1.length() - o2.length();
              }
          }

          posted on 2007-12-20 20:39 Jeff Lau 閱讀(2877) 評論(0)  編輯  收藏 所屬分類: 跟老劉學Java
          主站蜘蛛池模板: 新丰县| 奉节县| 广水市| 永吉县| 岐山县| 阜城县| 淮安市| 上杭县| 吉林省| 九龙县| 昭通市| 伊宁县| 儋州市| 庆元县| 桐城市| 富川| 沈丘县| 瑞昌市| 两当县| 玉环县| 田东县| 岱山县| 凯里市| 故城县| 仙居县| 通许县| 惠东县| 陈巴尔虎旗| 三原县| 吴堡县| 宁晋县| 西平县| 沂水县| 定安县| 中江县| 新昌县| 桦南县| 台东市| 会宁县| 东兰县| 临泉县|