首页 > 编程 > Java > 正文

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

2019-11-26 14:25:50
字体:
来源:转载
供稿:网友

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

具体代码如下所示:

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;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn "书号"+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); } }@Overridepublic int compare(Book book, Book book) {// TODO Auto-generated method stubint compare_value=collator.compare(book.getBook_name(),book.getBook_name());if(compare_value>){return ;}if(compare_value<){return -;}return ;}}

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表