Java实现对中文字符串的排序功能实例代码

废话不多说了,直接给大家代码分享代码了。

具体代码如下所示:

package test;
/**
* 
* @Title 书的信息类
* @author LR
* @version .
* @since --
*/
public class Book {
private String book_id;
private String book_name;
private String publishing_house;
public Book(String book_id, String book_name, String publishing_house) {
super();
this.book_id = book_id;
this.book_name = book_name;
this.publishing_house = publishing_house;
}
public String getBook_id() {
return book_id;
}
public void setBook_id(String book_id) {
this.book_id = book_id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getPublishing_house() {
return publishing_house;
}
public void setPublishing_house(String publishing_house) {
this.publishing_house = publishing_house;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "书号"+book_id+"\n书名"+book_name+"\n出版社"+publishing_house;
}
}

package test;

import java.text.Collator;
/**
* 
* @Title 中文字符串排序功能
* @author LR
* @version .
* @since --
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionChineseSort implements Comparator<Book>{
Collator collator= Collator.getInstance(java.util.Locale.CHINA);
public static void main(String[] args) {
ArrayList<Book> list=new ArrayList<Book>();
list.add(new Book("","英语","英语出版社"));
list.add(new Book("","日语","日语出版社"));
list.add(new Book("","德语","德语出版社"));
list.add(new Book("","法语","法语出版社"));
list.add(new Book("","俄语","俄语出版社"));
Collections.sort(list,new CollectionChineseSort());
for (Book book:list){ 
System.out.println(book); 
} 
}
@Override
public int compare(Book book, Book book) {
// TODO Auto-generated method stub
int compare_value=collator.compare(book.getBook_name(),book.getBook_name());
if(compare_value>){
return ;
}
if(compare_value<){
return -;
}
return ;
}
}

以上内容是小编给大家介绍的Java实现中文字符串的排序功能的代码,希望对大家有所帮助!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。