java用unicode而不是gbk,转换成gbk再比较

解决方案 »

  1.   

    JAVA内部是UNICODE编码,用String.getByte("编码")把字符串转换成指定编码
      

  2.   

    Unicode编码里面,没有拼音的顺序?转换了,还是不行!
      

  3.   

    public class encode {
    public static void main(String[] args){
    byte[] bys1 = "路".getBytes();
    byte[] bys2 = "座".getBytes(); System.out.println("compare0 == " + ("路".compareTo("座"))); //12104 
    String str1=null,str2=null;
    try{
    str1 = new String(bys1,"utf8");
    str2 = new String(bys2,"utf8");
    System.out.println("compare1 == " + (str1.compareTo(str2)));//-65350
    }catch(Exception e){
      
    }
    try{
    str1 = new String(bys1,"UTF-16BE");
    str2 = new String(bys2,"UTF-16BE");
    System.out.println("compare2 == " + (str1.compareTo(str2))); //-5442
    }catch(Exception e){
      System.out.println(e);
    } try{
    str1 = new String(bys1,"ISO-8859-1");
    str2 = new String(bys2,"ISO-8859-1");
    System.out.println("compare3 == " + (str1.compareTo(str2)));//-21
    }catch(Exception e){
      
    }
     
    }
    }
      

  4.   

    package tests.tips;import java.util.*;
    import java.text.CollationKey;
    import java.text.Collator;
    public class ChineseSortSample
    {    public ChineseSortSample()
        {
        }    public void normalSort(ArrayList arrayList)
        {
            Collections.sort(arrayList);
            System.out.println("normal sort result:");
            System.out.println(arrayList);
        }    public void chineseSort(ArrayList arrayList)
        {
            ArrayList newArray = new ArrayList();
            //liucheng, see this the following lines
            Collator collator = Collator.getInstance(Locale.CHINA);
            for (int i = 0; i < arrayList.size(); i++) {
                CollationKey key = collator.getCollationKey( (String) arrayList.get(
                    i));
                newArray.add(key);
            }
            Collections.sort(newArray);
            System.out.println("chinese sort result:");
            boolean first = true;
            Iterator iterator = newArray.iterator();
            System.out.print("[");
            while (iterator.hasNext()) {
                if (first) {
                    first = false;
                } else {
                    System.out.print(", ");
                }
                CollationKey key = (CollationKey) iterator.next();
                System.out.print(key.getSourceString());
            }
            System.out.println("]");
            //end
        }    public void show(ArrayList arrayList)
        {
            normalSort(arrayList);
            chineseSort(arrayList);
        }    public static void main(String[] args)
        {
            String testStr;
            if (args.length > 0) {
                testStr = args[0];
            } else {
                testStr = "\u8FD9\u662F\u4E00\u4E2A\u6D4B\u8BD5\u7A0B\u5E8F\uFF0C\u770B\u662F\u5426\u80FD\u591F\u6309\u7167\u4E2D\u6587\u62FC\u97F3\u7684\u82F1\u6587\u5B57\u6BCD\u6765\u6392\u5E8F";
            }
            ChineseSortSample chineseSortSample1 = new ChineseSortSample();        char[] charArray = testStr.toCharArray();
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < charArray.length; i++) {
                arrayList.add(String.valueOf(charArray[i]));
            }
            chineseSortSample1.show(arrayList);
        }
    }