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

时间:2021-05-19

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

具体代码如下所示:

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章