dingfirst

          On the Road

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            8 隨筆 :: 2 文章 :: 3 評論 :: 0 Trackbacks
          ?1 ????public?static?int?compare(String?str1,?String?str2)
          ?2?????{
          ?3?????????int?result?=?0;
          ?4?????????String?m_s1?=?null;
          ?5?????????String?m_s2?=?null;
          ?6?????????try
          ?7?????????{
          ?8?????????????m_s1?=?new?String(str1.getBytes(_FromEncode_),?_ToEncode_);
          ?9?????????????m_s2?=?new?String(str2.getBytes(_FromEncode_),?_ToEncode_);
          10?????????}

          11?????????catch(Exception?e)
          12?????????{
          13?????????????return?str1.compareTo(str2);
          14?????????}

          15?????????result?=?chineseCompareTo(m_s1,?m_s2);
          16?????????return?result;
          17?????}

          18
          19?????public?static?int?getCharCode(String?s)
          20?????{
          21?????????if(s?==?null?&&?s.equals(""))
          22?????????????return?-1;
          23?????????byte?b[]?=?s.getBytes();
          24?????????int?value?=?0;
          25?????????for(int?i?=?0;?i?<?b.length?&&?i?<=?2;?i++)
          26?????????????value?=?value?*?100?+?b[i];
          27
          28?????????return?value;
          29?????}

          30
          31?????public?static?int?chineseCompareTo(String?s1,?String?s2)
          32?????{
          33?????????int?len1?=?s1.length();
          34?????????int?len2?=?s2.length();
          35?????????int?n?=?Math.min(len1,?len2);
          36?????????for(int?i?=?0;?i?<?n;?i++)
          37?????????{
          38?????????????int?s1_code?=?getCharCode(s1.charAt(i)?+?"");
          39?????????????int?s2_code?=?getCharCode(s2.charAt(i)?+?"");
          40?????????????if(s1_code?*?s2_code?<?0)
          41?????????????????return?Math.min(s1_code,?s2_code);
          42?????????????if(s1_code?!=?s2_code)
          43?????????????????return?s1_code?-?s2_code;
          44?????????}

          45
          46?????????return?len1?-?len2;
          47?????}

          算是比較準的了,但還是有錯誤,而且性能比較差。

          算法最初是誰寫的也忘了。呵呵,拿來用一下.
          posted on 2006-07-11 15:18 dingfirst 閱讀(252) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 襄汾县| 鸡东县| 平罗县| 榆社县| 小金县| 公主岭市| 布尔津县| 永安市| 昌邑市| 鲁山县| 大余县| 垦利县| 永嘉县| 庐江县| 普陀区| 伊金霍洛旗| 化州市| 桐柏县| 永嘉县| 聂荣县| 合肥市| 华阴市| 大丰市| 丰原市| 扶绥县| 南木林县| 广丰县| 新和县| 胶州市| 邯郸市| 金湖县| 清新县| 习水县| 盘山县| 邹平县| 永城市| 杨浦区| 光泽县| 微山县| 吴桥县| 稷山县|