Java implements the Chinese string sorting function example code

  • 2020-05-09 18:32:12
  • OfStack

Without further ado, I'm going to share my code with you.

The specific code is as follows:


package test;
/**
* 
* @Title  Book information category 
* @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 number "+book_id+"\n Title: "+book_name+"\n Press. "+publishing_house;
}
}

package test;


import java.text.Collator;
/**
* 
* @Title  Chinese string sorting function 
* @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(""," English "," English press "));
list.add(new Book(""," Japanese "," Japanese publishing house "));
list.add(new Book(""," German "," German press "));
list.add(new Book(""," French "," French press "));
list.add(new Book(""," Russian "," Russian publishing house "));
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 ;
}
}

The above content is the site for you to introduce Java Chinese string sorting function code, I hope to help you!


Related articles: