解决extremeComponents中文按拼音排序问题
生活随笔
收集整理的這篇文章主要介紹了
解决extremeComponents中文按拼音排序问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章借鑒以下文章:
pinyin4j項目:http://pinyin4j.sourceforge.net/
extremeComponents排序實現不區分大小:http://www.blogjava.net/fastzch/archive/2006/04/07/39918.html
Java中文排序完美新解:http://blog.csdn.net/dada9407/article/details/2975622
?
向工程中加入pinyin4j-2.5.0.jar(文末提供pinyin4j-2.5.0.jar和pinyin4j-2.5.0.zip)。
把pinyin4j-2.5.0.jar拷入WebRoot/WEB-INF/lib,并添加到classpath。
?
在jsp頁面標簽中需指明排序所用的接口:sortRowsCallback="com.heer.MySortCallback"
?
<ec:table tableId="statList"retrieveRowsCallback="limit"filterRowsCallback="limit"var="result"items="resultList"action="${pageContext.request.contextPath}/stat.do?method=${actionMethod}"title="統計結果"width="100%"rowsDisplayed="10"filterable="false"sortRowsCallback="com.heer.MySortCallback">?
?
定義自己的Comparator類
?
package com.heer;import java.text.Collator; import java.util.Comparator; import net.sourceforge.pinyin4j.PinyinHelper; import org.apache.commons.collections.comparators.NullComparator;public class MyComparator extends NullComparator {Collator collator = Collator.getInstance();public int compare(Object o1, Object o2) {String key1 = o1 == null ? "" : o1.toString();String key2 = o2 == null ? "" : o2.toString();if(isNumeric(key1)&&isNumeric(key2)){return (int) (Double.valueOf(key1)-Double.valueOf(key2));}else{for (int i = 0; i < key1.length() && i < key2.length(); i++) {int codePoint1 = key1.charAt(i);int codePoint2 = key2.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 key1.length() - key2.length();}private String pinyin(char c) {String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);if (pinyins == null) {return null; // 如果轉換結果為空,則返回null}return pinyins[0]; // 如果為多音字返回第一個音節}public static boolean isNumeric(String str) {int begin = 0;boolean once = true;if (str == null || str.trim().equals("")) {return false;}str = str.trim();if (str.startsWith("+") || str.startsWith("-")) {if (str.length() == 1) {// "+" "-"return false;}begin = 1;}for (int i = begin; i < str.length(); i++) {if (!Character.isDigit(str.charAt(i))) {if (str.charAt(i) == '.' && once) {// '.' can only onceonce = false;} else {return false;}}}if (str.length() == (begin + 1) && !once) {// "." "+." "-."return false;}return true;} }?
?
?實現SortRowsCallback接口
package com.heer;import java.util.Collection; import java.util.Collections; import java.util.List;import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.comparators.ReverseComparator; import org.extremecomponents.table.bean.Column; import org.extremecomponents.table.callback.SortRowsCallback; import org.extremecomponents.table.core.TableConstants; import org.extremecomponents.table.core.TableModel; import org.extremecomponents.table.limit.Sort;public final class MySortCallback implements SortRowsCallback {public Collection sortRows(TableModel model, Collection rows)throws Exception {boolean sorted = model.getLimit().isSorted();if (!sorted) {return rows;}Sort sort = model.getLimit().getSort();String sortProperty = sort.getProperty();String sortOrder = sort.getSortOrder();Column column = model.getColumnHandler().getColumnByAlias(sortProperty);String property = column.getProperty();if (sortOrder.equals(TableConstants.SORT_ASC)) {BeanComparator comparator = new BeanComparator(property,new MyComparator());Collections.sort((List) rows, comparator);} else if (sortOrder.equals(TableConstants.SORT_DESC)) {BeanComparator reversedNaturalOrderBeanComparator = new BeanComparator(property, new ReverseComparator(new MyComparator()));Collections.sort((List) rows, reversedNaturalOrderBeanComparator);}return rows;}}?
這樣常用漢字和非常用都會取漢字的漢語拼音,再按照英文字符的比較方法進行排序。
?
總結
以上是生活随笔為你收集整理的解决extremeComponents中文按拼音排序问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法与数据结构1800题 树和二叉树
- 下一篇: Android键盘映射