服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java实现对中文字符串的排序功能实例代码

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

2020-04-19 12:12淡纷飞菊 JAVA教程

这篇文章主要介绍了Java实现中文字符串的排序功能实例代码的相关资料,需要的朋友可以参考下

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

具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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实现中文字符串的排序功能的代码,希望对大家有所帮助!

延伸 · 阅读

精彩推荐