利用PinYin4j 实现List中的对象按数字,字母, 汉字排序
要排序的对象:
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; public class A implements Comparable<A>{
private String name; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(A o) {
String regex = "^\\w.*";
if (this.getName().matches(regex) || o.getName().matches(regex)) {
return this.getName().compareTo(o.getName());
} else {
return genPinYin(this.getName()).compareTo(genPinYin(o.getName()));
}
}
public static String genPinYin(String input) {
if (input == null || input.trim().equals("")) {
return "";
}
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
//多音字预先转换 这里可以处理一下多音字
char[] chars = input.trim().toCharArray();
StringBuilder output = new StringBuilder();
try {
for (char c : chars) {
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format);
output.append(temp[0]);
} else {
output.append(Character.toString(c));
}
}
} catch (Exception e) {
System.err.println("拼音转换出现未知异常:" + input);
}
return output.toString();
} }
排序实现:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Test { public static void main(String[] args) {
A a1 = new A(); A a2 = new A();
A a3 = new A(); A a4 = new A();
A a5 = new A(); A a6 = new A();
A a7 = new A(); A a8 = new A(); a1.setName("二维码"); a2.setName("回调");
a3.setName("接口"); a4.setName("服务窗");
a5.setName("123"); a6.setName("456");
a7.setName("zbc"); a8.setName("Abhj"); List<A> list = new ArrayList<A>();
list.add(a1); list.add(a2); list.add(a3); list.add(a4);
list.add(a5); list.add(a6); list.add(a7); list.add(a8); Collections.sort(list); for (A a : list) {
System.out.println(a.getName());
}
}
}
jar 包下载地址: http://pan.baidu.com/s/1sj9A2wl
利用PinYin4j 实现List中的对象按数字,字母, 汉字排序的更多相关文章
- 如何对List集合中的对象进行按某个属性排序
我们在实际的开发工作中,经常会碰到排序的问题,如题,我们如何针对List集合中的某一个属性进行排序 当list集合中的元素类型是我们自定义类型时,有两种对list中的元素进行排序的方法: 方法一 让l ...
- javascript 中关于对象转换数字值的一些特点
下面是摘至<Javascript 高级程序设计第三版>里的一段话 是关于对象转换数字值的一些规则 "在应用于对象时,先调用对象的valueOf()方法以取得一个可供操作的值.然后 ...
- 对List中每个对象元素按时间顺序排序
需求: 需要对List中的每个User按照birthday顺序排序,时间由小到大排列. 代码实现: import java.text.SimpleDateFormat; import java.uti ...
- coding++:对List中每个对象元素按时间顺序排序
需求: 需要对List中的每个User按照birthday顺序排序,时间由小到大排列. package com.tree.ztree_demo.orderby; import java.text.Si ...
- 【java】之对List中每个对象元素按时间顺序排序
import java.text.SimpleDateFormat; import java.util.*; public class ListSort { public static class U ...
- 使用sort对数组中的对象的某个属性进行排序
var data=[ { code: "10004", grade: "5.6", planQuantity: 220, }, { code: "10 ...
- python 利用split读取文本文件中每一行的数字并保存至相应文件夹
import re from numpy import * def getStr(file_path,file_path1): fp = open(file_path, 'r') op = open( ...
- 关于java中实现在oracle数据库中实现对中文首字母进行排序的解决方案
首先介绍Oracle 9i新增加的一个系统自带的排序函数 1.按首字母排序 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值 SCHINESE_RADICAL_M ...
- 【学习笔记】Java中生成对象的5中方法
概述:本文介绍以下java五种创建对象的方式: 1.用new语句创建对象,这是最常用的创建对象的方式. 2.使用Class类的newInstance方法 3.运用反射手段,调用java.lang.re ...
随机推荐
- memcache缓存详解
这篇文章主要介绍了PHP中的Memcache,从Memcache简介开始,详细讲解了如Memcache和memcached的区别.PHP的 Memcache所有操作方法.每个操作方法的详细解释等,需要 ...
- destoon 会员整合Ucenter/Discuz!/PHPWind教程
首先进入 Destoon网站后台 -〉会员管理 -〉模块设置 -〉会员整合 假如需要整合的主站地址为 http://www.abc.com 论坛为 http://bbs.abc.com 1.整合Uce ...
- jquery与服务器交换数据的利器--ajax(异步javascript and xml)
load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 一.下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载 ...
- [HTML]HTML框架IFrame下利用JS在主页面和子页面间传值
今天写的程序涉及到JS框架传值的问题,这些是我找到的一些资料 下面主页面和子页面互相传值的DEMO 如果仅仅需要子页面触发主页面的函数 仅需 [ parent.window.你的函数 ] 就可以了 D ...
- java下载安装,环境变量,hello world
1.Java下载安装 网址:http://java.sun.com/javase/downloads/index.jsp win7 64位选择jdk-8u11-windows-x64.exe. 2.环 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 周赛-Equidistant String 分类: 比赛 2015-08-08 15:44 6人阅读 评论(0) 收藏
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 在repeart中获取行数据
ItemCreated(){ if (e.Item.DataItem != null) { string examTypeId = ((DataRowView)e.Item.DataItem).Row ...
- ural 1113,jeep problem
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1113 网上的解答铺天盖地.我硬是花了两天才懂了点. wiki上的解释:https://e ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...