- 浏览: 998312 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (1355)
- test (75)
- 红茶和绿茶 (1)
- Jave SE (206)
- Oracle (19)
- English (177)
- Log4j (5)
- RIA(Rich Internet Applications) (9)
- Ext Js (6)
- Android (14)
- Logo (0)
- 文字采撷 (287)
- 使用技巧 (92)
- Project Management (22)
- Hibernate (12)
- Struts (5)
- 规则引擎 (1)
- Html & Javasctipt (56)
- Spring MVC (10)
- Maven (17)
- Java Test (17)
- Linux (16)
- Tools (1)
- CV (0)
- Middleware (2)
- HTML5 (2)
- Algorithms (4)
- Web Service (15)
- 留学 (15)
- LADP (5)
- PXCOA (0)
- SysLog (6)
- SSO (3)
- Spring Security (4)
- Spring Batch (1)
- Jmail (1)
- Bible (4)
- Java Thread (5)
- Architect (6)
- github (2)
- Java Swing (12)
- NoSQL (7)
- UML (2)
- 敏捷(Agile) (7)
- Hudson+Maven+SVN (15)
- cloud computing (2)
- Bahasa Indonesia (1)
- jBPM (6)
- 民俗知识 (3)
- Consulting (1)
- Mysql (5)
- SAP (1)
- 微信公众平台接口开发 (3)
- 做生意 (1)
- 西餐 (1)
- Banking (1)
- Flex (0)
- 黄金投资 (1)
- Apache Tomcat 集群 (3)
- Hadoop (7)
- 需求分析 (1)
- 银行知识 (3)
- 产品管理 (2)
- 钢琴Music (3)
- 设计 (3)
- Marketing (2)
- US Life (3)
- 算法 (14)
- BigData (4)
- test红茶和绿茶Jave SEOracleEnglishLog4jRIA(Rich Internet Applications)Ext JsAndroidLogo文字采撷 (0)
- Design Pattern (5)
- NodeJS&AngularJS (9)
- Python (1)
- Spring boot (0)
- ACM (3)
最新评论
-
心往圣城:
微时代-最专业的微信第三方平台。LBS定位导航,微网站,自定义 ...
微信公众平台 /微信公众平台怎么用 -
zhaojiafan:
return ReverseStr1(str.substrin ...
逆转字符串 Write a String Reverser (and use Recursion!) -
zhaojiafan:
public class StringUtils {
p ...
逆转字符串 Write a String Reverser (and use Recursion!)
对Search进行排序
SearchComparator.java中的实现方法compare已不能满足需要
其中涉及到中文、英文或者中英文混合排序,所以,这里使用开源的Pingyin 对起排序
SearchComparator.java调用sortListByType排序,其中调用了PinyinComparator
SearchComparator .java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.clx.webmail.util.PinyinComparator;
public class SearchComparator implements Comparator<Search> {
/**简单,不能用来对纯中文、纯英文或者中英文混合进行排序**/
public int compare(Search s1, Search s2) {
return s1.getSearchName().compareToIgnoreCase(s2.getSearchName());
}
public List<Search> sortListByType(List<Search> list)
{
List engList=new ArrayList();
List chaList=new ArrayList();
if(list!=null&&list.size()>0)
{
for(int i=0;i<list.size();i++)
{
Search search=(Search)list.get(i);
String name=search.getSearchName();
/**
* 如果英文优先,则这里的IF条件为:
* isContainsHanyu(name.substring(0,1))&&isContainsHanyu(name)
**/
if(isContainsHanyu(name))
{
chaList.add(search);
}
else
{
engList.add(search);
}
}
}
if(chaList.size()>0)
{
PinyinComparator pinyin=new PinyinComparator();
Collections.sort(chaList,pinyin);
}
if(engList.size()>0)
{
Collections.sort(engList,this);
}
list=new ArrayList();
list=copy(list,engList);
list=copy(list,chaList);
return list;
}
//把一个集合中的元素复制到另一个集合中
public List copy(List sourceList,List copyList)
{
if(copyList!=null)
{
for(int i=0;i<copyList.size();i++)
{
sourceList.add(copyList.get(i));
}
}
return sourceList;
}
//检查字符串是否包含中文
public boolean isContainsHanyu(String str)
{
boolean flag=false;
Pattern pattern = Pattern.compile("[//u4E00-//u9FA5]+",Pattern.CANON_EQ);
Matcher matcher = pattern.matcher(str);
if(matcher.find())
{
flag=true;
}
return flag;
}
public static void main(String[] args)
{
String s = "test测试";
SearchComparator comparator=new SearchComparator();
comparator.isContainsHanyu(s);
}
}
PinyinComparator.java
import java.util.Arrays;
import java.util.Comparator;
import com.clx.webmail.models.Search;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinyinComparator implements Comparator<Search>{
public int compare(Search s1,Search s2) {
String o1=s1.getSearchName();
String o2=s2.getSearchName();
for (int i = 0; i < o1.length() && i < o2.length(); i++) {
int codePoint1 = o1.charAt(i);
int codePoint2 = o2.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 o1.length() - o2.length();
}
/**对中英文排序**/
private String pinyin(char c) {
if(String.valueOf(c)==null||String.valueOf(c).length()==0)
{
return "";
}
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
String output="";
try
{
if (java.lang.Character.toString(c).matches("[//u4E00-//u9FA5]+"))
{
String[] temp = PinyinHelper.toHanyuPinyinStringArray(c,format);
if(temp!=null&&temp.length>0)
{
output += temp[0];
}
}
else
{
output += java.lang.Character.toString(c);
}
}catch(BadHanyuPinyinOutputFormatCombination e)
{
e.printStackTrace();
}
return output;
}
}
此外,增加一个类,此类用来得到汉语拼音,可能在排序中也用得到
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.io.UnsupportedEncodingException;
/**
* 拼音工具
*
* @author zhouhang 2010-01-25
*/
public class PinyinToolkit {
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音首字母
*/
public static String cn2FirstSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128)
{
try {
String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (_t != null) {
pybf.append(_t[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().replaceAll("//W", "").trim();
}
/**
* 获取汉字串拼音,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音
*/
public static String cn2Spell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException {
String x = "嘅囧誰說壞學生來勼髮視頻裆児";
System.out.println(cn2FirstSpell(x));
System.out.println(cn2Spell(x));
}
}
转自: http://blog.csdn.net/shenzhen_mydream/article/details/5253048
发表评论
-
各种在线工具
2018-05-10 05:52 378http://rextester.com/ -
Java Array sort and Collections sort
2018-04-11 04:55 345package com.test; imp ... -
webpack+es6+node+react初实践及总结
2018-02-01 10:38 322webpack+es6+node+react初实践及总结 ... -
Interview Preparation
2018-01-25 08:26 397Algorithms https://www. ... -
深入理解Java集合框架
2017-08-18 08:40 577https://github.com/CarpenterLe ... -
logic gate (AND, OR, XOR, NOT, NAND, NOR and XNOR)
2017-08-18 08:33 2357A logic gate is an elementary ... -
深入理解Java PriorityQueue
2017-08-18 01:25 385本文github地址 Java中PriorityQueu ... -
jwt-spring-security-demo
2017-08-12 07:30 550https://github.com/szerh ... -
Java Program to Check Whether a Number is Palindrome or Not
2017-08-08 06:59 501public class Palindrome { ... -
Java实现Tire
2017-08-07 08:14 546Java实现Tire Trie ... -
OpenID, SAML, and OAuth
2017-08-03 07:03 551Single sign-on (SSO) started i ... -
分享两个JavaEE 非常好的网站,案例丰富
2017-08-01 09:07 300http://www.mkyong.com/al ... -
Introduction to Programming in Java
2017-07-19 13:26 413http://introcs.cs.princeton.ed ... -
Two piece of code
2017-06-20 00:43 390if ( updateRe ... -
ACM Online Judge
2017-06-05 01:26 418http://acm.nyist. ... -
java枚举使用详解
2017-05-25 06:16 423package com.ljq.test; /** ... -
Longest Common Substring
2017-05-21 08:22 464Dynamic Programming | Set 29 ( ... -
Dynamic Programming
2017-05-06 10:48 328Dynamic Programming | Set 1 (O ... -
Predefined Character Classes
2017-04-24 02:45 365Predefined Character Clas ... -
IS-A Relationship And HAS-A Relationship
2017-04-13 14:50 1132One of the advantages of an Ob ...
相关推荐
一个简单的demo,关于中英文排序,一个java Demo
Android中英文混合排序,中文的拼音与英文字符串比较后排序
js实现中英文混合排序,支持所有浏览器,包括谷歌
通讯录,最简单的中英文混合排序封装,微信通讯录 demo!
本文实例讲述了Java编程实现中英混合字符串数组按首字母排序的方法。分享给大家供大家参考,具体如下: 在Java中对于字符串数组的排序,我们可以使用Arrays.sort(String[])方法很便捷的进行排序。例如: String[]...
主要为大家详细介绍了Android实现列表数据按名称排序、中英文混合排序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
List<String> location=new ArrayList(); 一行代码轻松搞定混合排序问题 Collections.sort(location, new SortUtils(true));
主要介绍了小程序中英文混合排序问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了基于JS实现数字+字母+中文的混合排序方法的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
实现的功能:混合字符串处理 中英文排序 右侧悬浮栏 顶部名称提示栏 滚动悬浮提示,文件里放置了运行的效果图,实现的效果还是让人很满意的 我开发的环境是intellij idea, 导入到eclipse要注意处理一些问题,具体...
使用elementUI中的table时,给包含数字字母中文的名称等字段排序 例如:数字(0->9)->大写字母(A->Z)->小写字母(a->z)->中文拼音(a->z) 2.代码解析 tooltip-effect=dark class=xg-table xss=removed max...
IOS根据汉字(或英文)按字母排序并按首字母分组
flutter
内容索引:脚本资源,Ajax/JavaScript,表格排序,表头 JavaScript表格排序,点击表头即可排序,此脚本支持中文汉字排序、中英文混合排序、数据大小排序、文件名称类型排序、日期排序、价格排序、中文和数字排序;...
android ListView列表根据字母排序和定位,支持混合排序:混合字符串处理、中英文排序、右侧悬浮栏、顶部名称提示栏、滚动悬浮提示,运行效果图一看便知,目前Android机常见的通讯录搜索功能,本例模拟实现的效果...