摘要:在很多中文書籍的作者頁中,有很多作者的話,很多時候,是按作者姓名的筆畫順序排序的。Microsoft的Excel和Sql Server實現了按筆畫排序的功能。那按筆畫順序排序,在Java中究竟怎樣實現呢?
作者:Jeff 發表于:2007-12-20 20:39 最后更新于: 2007年12月21日 12:17
版權聲明:可以任意轉載,轉載時請務必以超鏈接形式標明文章原始出處和作者信息及本版權聲明。
http://m.tkk7.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 閱讀(2873)
評論(0) 編輯 收藏 所屬分類:
跟老劉學Java