欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 14:58:25
废话不多说了,直接给大家代码分享代码了。 具体代码如下所示: package test; /** * * @title 书的信息类 * @auth...

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

具体代码如下所示:

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实现中文字符串的排序功能的代码,希望对大家有所帮助!