解决extremeComponents中文按拼音排序问题_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 解决extremeComponents中文按拼音排序问题

解决extremeComponents中文按拼音排序问题

 2013/9/29 18:53:57  disneys  程序员俱乐部  我要评论(0)
  • 摘要:文章借鉴一下文章:pinyin4j项目:http://pinyin4j.sourceforge.net/extremeComponents排序实现不区分大小:http://www.blogjava.net/fastzch/archive/2006/04/07/39918.htmlJava中文排序完美新解:http://blog.csdn.net/dada9407/article/details/2975622向工程中加入pinyin4j-2.5.0.jar(文末提供pinyin4j-2.5.0
  • 标签:解决 问题

文章借鉴一下文章:

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页面标签中需指明排序所用的接口monospace; line-height: 1.5; color: #ff0000;">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) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				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;
	}

}

?

这样常用汉字和非常用都会取汉字的汉语拼音,再按照英文字符的比较方法进行排序。

?

  • pinyin4j-2.5.0.jar (184.5 KB)
  • 下载次数: 1
  • pinyin4j-2.5.0.zip (360.2 KB)
  • 下载次数: 0
发表评论
用户名: 匿名