本站首页    管理页面    写新日志    退出


«November 2025»
1
2345678
9101112131415
16171819202122
23242526272829
30


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7664665
建立时间:2006年5月29日




[算法]实现对中文字符串数组按照音序排列 
软件技术

lhwork 发表于 2006/8/23 9:45:16

public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }}调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator());for(int len=0;len<str.length;len++){                       System.out.println(str[len]); }


阅读全文(3038) | 回复(1) | 编辑 | 精华
 


回复:实现对中文字符串数组按照音序排列
软件技术

www(游客)发表评论于2006/10/12 18:00:20

public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); } public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); }oo   public class SortComparator implements Comparator{    public int compare(Object o1,Object o2) {        try{                byte[] buf1 = ((String) o1).getBytes("unicode");                byte[] buf2 = ((String) o2).getBytes("unicode");                int size = Math.min(buf1.length, buf2.length);                for (int i = 0; i < size; i++) {                    if (buf1[i] < buf2[i])                        return -1;                    else if (buf1[i] > buf2[i])                        return 1;                }                return buf1.length - buf2.length;            }catch(UnsupportedEncodingException ex) {                return 0;            }    }} 调用:String[] str = {"北京","中国","亚运会"};Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){                       System.out.println(str[len]); }  


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.969 second(s), page refreshed 144822587 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号