package Util;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.pinyin4j.PinyinHelper;
import Bean.Director;
import Bean.MovieInfo;
public class SortDirector implements Comparator<Object>{
public int compare(Object o1,Object o2) {
Director l1 = (Director) o1;
Director l2 = (Director) o2;
String name1 = l1.getName().toString();
String name2 = l2.getName().toString();
if("".equals(name1)){
return 0;
}
if("".equals(name2)){
return 0;
}
if(isChineseChar(name1.charAt(0)+"")&&isChineseChar(name2.charAt(0)+"")){
for (int i = 0; i < name1.length() && i < name2.length(); i++) {
int codePoint1 = name1.charAt(i);
int codePoint2 = name2.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;
}
}
}
}
if(isChineseChar(name1.charAt(0)+"")&!isChineseChar(name2.charAt(0)+"")){
return -1;
}
if(!isChineseChar(name1.charAt(0)+"")&isChineseChar(name2.charAt(0)+"")){
return 1;
}
return name1.charAt(0) - name2.charAt(0);
}
/**
* 字符的拼音,多音字就得到第一个拼音。不是汉字,就return null。
*/
private String pinyin(char c) {
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
if (pinyins == null) {
return null;
}
return pinyins[0];
}
public static void testNew(){
List<Director> chinesesOrderList = new ArrayList< Director>();
Director a = new Director();
Director a1 = new Director();
Director a2 = new Director();
Director a3 = new Director();
Director a4 = new Director();
Director a5 = new Director();
Director a6 = new Director();
Director a7 = new Director();
Director a8 = new Director();
Director a9 = new Director();
Director a0 = new Director();
a.setName("");
a0.setName("d");
a1.setName("v ");
a2.setName("座头市");
a3.setName("倩女幽魂");
a4.setName("喋血双雄");
a5.setName("飓风");
a6.setName("艋舺");
a7.setName("测试部门");
a8.setName("楢山节考");
a9.setName("蝙蝠侠:黑暗");
chinesesOrderList.add(a);
chinesesOrderList.add(a0);
chinesesOrderList.add(a1);
chinesesOrderList.add(a2);
chinesesOrderList.add(a3);
chinesesOrderList.add(a4);
chinesesOrderList.add(a5);
chinesesOrderList.add(a6);
chinesesOrderList.add(a7);
chinesesOrderList.add(a8);
chinesesOrderList.add(a9);
Collections.sort(chinesesOrderList,new SortDirector());
System.out.println("中文排序: = ");
for (int i = 0; i < chinesesOrderList.size(); i++) {
Director chinese = chinesesOrderList.get(i);
System.out.println("" + chinese.getName());
}
}
public static void main(String[] args) {
testNew();
}
public static boolean isChineseChar(String str){
boolean temp = false;
Pattern p=Pattern.compile("[\u4e00-\u9fa5]");
Matcher m=p.matcher(str);
if(m.find()){
temp = true;
}
return temp;
}
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.pinyin4j.PinyinHelper;
import Bean.Director;
import Bean.MovieInfo;
public class SortDirector implements Comparator<Object>{
public int compare(Object o1,Object o2) {
Director l1 = (Director) o1;
Director l2 = (Director) o2;
String name1 = l1.getName().toString();
String name2 = l2.getName().toString();
if("".equals(name1)){
return 0;
}
if("".equals(name2)){
return 0;
}
if(isChineseChar(name1.charAt(0)+"")&&isChineseChar(name2.charAt(0)+"")){
for (int i = 0; i < name1.length() && i < name2.length(); i++) {
int codePoint1 = name1.charAt(i);
int codePoint2 = name2.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;
}
}
}
}
if(isChineseChar(name1.charAt(0)+"")&!isChineseChar(name2.charAt(0)+"")){
return -1;
}
if(!isChineseChar(name1.charAt(0)+"")&isChineseChar(name2.charAt(0)+"")){
return 1;
}
return name1.charAt(0) - name2.charAt(0);
}
/**
* 字符的拼音,多音字就得到第一个拼音。不是汉字,就return null。
*/
private String pinyin(char c) {
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
if (pinyins == null) {
return null;
}
return pinyins[0];
}
public static void testNew(){
List<Director> chinesesOrderList = new ArrayList< Director>();
Director a = new Director();
Director a1 = new Director();
Director a2 = new Director();
Director a3 = new Director();
Director a4 = new Director();
Director a5 = new Director();
Director a6 = new Director();
Director a7 = new Director();
Director a8 = new Director();
Director a9 = new Director();
Director a0 = new Director();
a.setName("");
a0.setName("d");
a1.setName("v ");
a2.setName("座头市");
a3.setName("倩女幽魂");
a4.setName("喋血双雄");
a5.setName("飓风");
a6.setName("艋舺");
a7.setName("测试部门");
a8.setName("楢山节考");
a9.setName("蝙蝠侠:黑暗");
chinesesOrderList.add(a);
chinesesOrderList.add(a0);
chinesesOrderList.add(a1);
chinesesOrderList.add(a2);
chinesesOrderList.add(a3);
chinesesOrderList.add(a4);
chinesesOrderList.add(a5);
chinesesOrderList.add(a6);
chinesesOrderList.add(a7);
chinesesOrderList.add(a8);
chinesesOrderList.add(a9);
Collections.sort(chinesesOrderList,new SortDirector());
System.out.println("中文排序: = ");
for (int i = 0; i < chinesesOrderList.size(); i++) {
Director chinese = chinesesOrderList.get(i);
System.out.println("" + chinese.getName());
}
}
public static void main(String[] args) {
testNew();
}
public static boolean isChineseChar(String str){
boolean temp = false;
Pattern p=Pattern.compile("[\u4e00-\u9fa5]");
Matcher m=p.matcher(str);
if(m.find()){
temp = true;
}
return temp;
}
}
运行结果如下·: