Implementing Chinese sorting in Java programming

From , 5 Years ago, written in Java, viewed 76 times.
URL https://pastebin.vip/view/33a5435d
  1. 第一种情况: Comparatorcmp=Collator.getInstance(java.util.Locale.CHINA);
  2.                      String[]arr={"张三","李四","王五","刘六"};
  3.                      Arrays.sort(arr,cmp);
  4.                      for(inti=0;iarr.length;i++)                  
  5.                   System.out.println(arr[i]);
  6. 第二种情况: ComparableBean.java importjava.
  7.  
  8.  
  9. 第一种情况:
  10.        Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);  
  11.  
  12.         String[] arr = { \"张三\", \"李四\", \"王五\", \"刘六\" };
  13.  
  14.       Arrays.sort(arr, cmp);
  15.  
  16.      for (int i = 0; i < arr.length; i++)
  17.  
  18.         System.out.println(arr[i]);
  19.  
  20.  
  21.  
  22. 第二种情况:
  23.  
  24. //ComparableBean.java
  25. import java.text.CollationKey;
  26. import java.text.Collator;
  27. import java.text.RuleBasedCollator;
  28. import java.util.Comparator;
  29.  
  30. public class ComparableBean{
  31.    private String name;
  32.    
  33.    public ComparableBean(String name) {
  34.        
  35.        this.name = name;
  36.    }
  37.  
  38.    public String getName() {
  39.        return name;
  40.    }
  41.  
  42.    public void setName(String name) {
  43.        this.name = name;
  44.    }
  45. }
  46. class ComparableBeanComparator  implements Comparator//<ComparableBean>
  47. {
  48.    RuleBasedCollator collator; // you can set your rules for the instance \"collator\"
  49.    public ComparableBeanComparator()
  50.    {
  51.        collator = (RuleBasedCollator)Collator.getInstance(java.util.Locale.CHINA);// try testing various locales
  52.    }
  53.    public int compare(Object obj1, Object obj2) {
  54.        String tempname1 = ((ComparableBean) obj1).getName();
  55.        String tempname2 = ((ComparableBean) obj2).getName();
  56.        
  57.        CollationKey c1 = collator.getCollationKey(tempname1);
  58.        CollationKey c2 = collator.getCollationKey(tempname2);
  59.        return collator.compare(((CollationKey) c1).getSourceString(),
  60.                ((CollationKey) c2).getSourceString());
  61.       return collator.compare(((CollationKey) c2).getSourceString(),
  62.                ((CollationKey) c1).getSourceString());
  63.    }
  64.    public int compare(ComparableBean obj1, ComparableBean obj2) {
  65.        String tempname1 = obj1.getName();
  66.        String tempname2 = obj2.getName();
  67.        
  68.        CollationKey c1 = collator.getCollationKey(tempname1);
  69.        CollationKey c2 = collator.getCollationKey(tempname2);
  70.        return collator.compare(((CollationKey) c1).getSourceString(),
  71.                ((CollationKey) c2).getSourceString());
  72.  }
  73. }
  74.  
  75.  
  76. 测试
  77. //java/1346

Reply to "Implementing Chinese sorting in Java programming"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website