java实现中英混合排序

在使用前记得加载jar包()pinyin4i.jar

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 PingYinUtil {

public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);

char[] input = inputString.trim().toCharArray();
String output = "";

try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output += temp[0];
} else
output += java.lang.Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output;
}

}


import java.util.Comparator;

public class PinyinComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
String str1 = PingYinUtil.getPingYin((String) o1);
String str2 = PingYinUtil.getPingYin((String) o2);
return str1.compareTo(str2);
}
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test
{
@SuppressWarnings("unchecked")
public void sort()
{
List list = new ArrayList();
List templist = new ArrayList();
String[] array = { "陈少华", "熊易", "周武", "去情分", "aa", "dddd", "gggg",
"zzzz" };
for (int i = 0; i < array.length; i++)
{
java j = new java();
j.setName(array[i]);
j.setAge(10001 + i);
list.add(j);
}
@SuppressWarnings("rawtypes")
List list1 = Arrays.asList(array);
Arrays.sort(array, new PinyinComparator());
System.out.println(list1.size());
System.out.println(list.size());
for (int i = 0; i < list1.size(); i++)
{
for (int x = 0; x < list.size(); x++)
{
if (list1.get(i).toString().equals(list.get(x).getName()))
{
java ja = new java();
ja.setName(list1.get(i).toString());
ja.setAge(list.get(x).getAge());
templist.add(ja);
}
}
}
System.out.println(list1);

for (int i = 0; i < templist.size(); i++)
{
System.out.println(templist.get(i).getName() + " "
+ templist.get(i).getAge());
}
}

public static void main(String[] args)
{
new Test().sort();
}

class java
{
private int age;
private String name;

/**
* @return the age
*/
public int getAge()
{
return age;
}

/**
* @param age
* the age to set
*/
public void setAge(int age)
{
this.age = age;
}

/**
* @return the name
*/
public String getName()
{
return name;
}


/**
* @param name
* the name to set
*/
public void setName(String name)
{
this.name = name;
}

}
}


你可能感兴趣的:(android开发)