当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:16:00  来源:本站整理

Java中文排序[Java编程]

赞助商链接



  本文“Java中文排序[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
第一种情形:

  1. Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
  2.  
  3. String[] arr = { "张三", "李四", "王五", "刘六" };
  4. Arrays.sort(arr, cmp);
  5. for (int i = 0; i < arr.length; i++)
  6. System.out.println(arr[i]);


第二种情形:

//ComparableBean.java
import java.text.CollationKey;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Comparator;

public class ComparableBean{
private String name;

public ComparableBean(String name) {

this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
class ComparableBeanComparator implements Comparator//<ComparableBean>
{
RuleBasedCollator collator; // you can set your rules for the instance "collator"
public ComparableBeanComparator()
{
collator = (RuleBasedCollator)Collator.getInstance(java.util.Locale.CHINA);// try testing various locales
}
public int compare(Object obj1, Object obj2) {
String tempname1 = ((ComparableBean) obj1).getName();
String tempname2 = ((ComparableBean) obj2).getName();

CollationKey c1 = collator.getCollationKey(tempname1);
CollationKey c2 = collator.getCollationKey(tempname2);
// return collator.compare(((CollationKey) c1).getSourceString(),
// ((CollationKey) c2).getSourceString());
return collator.compare(((CollationKey) c2).getSourceString(),
((CollationKey) c1).getSourceString());
}
// public int compare(ComparableBean obj1, ComparableBean obj2) {
// String tempname1 = obj1.getName();
// String tempname2 = obj2.getName();
//
// CollationKey c1 = collator.getCollationKey(tempname1);
// CollationKey c2 = collator.getCollationKey(tempname2);
// return collator.compare(((CollationKey) c1).getSourceString(),
// ((CollationKey) c2).getSourceString());
// }
}
//the end of ComparableBean.java

测试代码:

ComparableBean[] nameContent = { new ComparableBean("一切从实际动身"),
new ComparableBean("立于不败之地"), new ComparableBean("多项式"),
new ComparableBean("贯彻落实"), new ComparableBean("密切接洽大众"),
new ComparableBean("四项基本原则"), new ComparableBean("咬牙切齿"),
new ComparableBean("必恭必敬"), new ComparableBean("民警"),
new ComparableBean("策划承包责任制") };
Arrays.sort(nameContent,new ComparableBeanComparator());
for (int i = 0; i < nameContent.length; i++) {
System.out.println(nameContent[i].getName());
}
  以上是“Java中文排序[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .