思路:将每个汉字都转换为一个汉字对象,利用汉字对象拥有的拼音属性排序.
import java.util.*;public class gb implements Comparable{
private String gb; //汉字
private String gbKey; //对应的拼音
gb(String gb1,String gbKey1) {
gb=gb1;
gbKey=gbKey1;
}
String getGbKey(){
return gbKey;
}
public int compareTo(Object o){
return gbKey.compareTo((gb)o.getGbKey())
} public void main(String[] args){
gb[] gbs=new gb[n]
........... //将汉字对象存入容器中
Arrays.sort(gbs) //排序
}
}如果只排序少许几个汉字还可以.
import java.util.*;public class gb implements Comparable{
private String gb; //汉字
private String gbKey; //对应的拼音
gb(String gb1,String gbKey1) {
gb=gb1;
gbKey=gbKey1;
}
String getGbKey(){
return gbKey;
}
public int compareTo(Object o){
return gbKey.compareTo((gb)o.getGbKey())
} public void main(String[] args){
gb[] gbs=new gb[n]
........... //将汉字对象存入容器中
Arrays.sort(gbs) //排序
}
}如果只排序少许几个汉字还可以.
private static String _FromEncode_ = "GBK";
private static String _ToEncode_ = "GBK";
protected gbkStringCompareTools() {
} public static int compare(String str1,String str2){
int result =0;
String m_s1 = null,m_s2 = null;
try {
m_s1 = new String(str1.getBytes(_FromEncode_),_ToEncode_);
m_s2 = new String(str2.getBytes(_FromEncode_),_ToEncode_);
} catch(Exception e){
return str1.compareTo(str2);
}
result = chineseCompareTo(m_s1,m_s2);
return result;
} public static int getCharCode(String s){
if (s == null && s.equals(""))
return -1;
byte[] b = s.getBytes();
int value = 0;
for (int i = 0; i < b.length && i <= 2; i++){
value = value*100 + b[i]; //*100左移
}
return value;
} public static int chineseCompareTo(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int n = Math.min(len1, len2);
for (int i = 0; i < n; i++) {
int s1_code = getCharCode(s1.charAt(i) + "");
int s2_code = getCharCode(s2.charAt(i) + "");
if (s1_code*s2_code<0)
return Math.min(s1_code,s2_code);
if (s1_code != s2_code)
return s1_code - s2_code;
}
return len1 - len2;
}
}
int revl = c.compare( "中", "华" );
System.out.println( "Res = " + revl );
//"中"的拼音排序在"华"之后啊,所以是小于,所以这里将输出-1这里实现了中文按照拼音比较大小
那么对于一列中文的排序只需要将这些中文放入一个List,然后调用 Collections.sort(List, Comparator) 这个方法既可
我很普通。不如你说的NB。
代码是按unicode编码排序的。
import java.util.Arrays;
import java.util.Comparator;
import java.util.*;
import java.util.Collections;public class sort {
public static void main( String[] args ) throws UnsupportedEncodingException {
String[] s = {"孙", "孟", "宋", "尹", "廖", "张", "张", "张", "徐", "昆", "曹", "曾"};
String[] c = new String[s.length];
for ( int i = 0; i < s.length; i++ ) {
c[i] = new String( s[i].getBytes("gbk"), "iso-8859-1" );
}
/*用数组排序
Arrays.sort( c, new Comparator() {
public int compare( Object o1, Object o2 ) {
return ( ( String ) o1 ).compareTo( ( String ) o2 );
}
}
);
*/
//Arrays.sort( c); //用集合排序
List list = new ArrayList();
for (int i = 0; i < c.length; i++) {
list.add(c[i]);
}
Collections.sort(list);
for (int i = 0; i < c.length; i++) {
c[i] = (String)list.get(i);
}
for ( int i = 0; i < c.length; i++ ) {
String cc = c[i];
System.out.println( new String( cc.getBytes( "iso-8859-1" ), "gbk" ) );
}
}
}