需求:由于需要对一些中文字符的章节进行排序,比如 第一章 第一十二章,第xx等
java自带的排序显示不能够解决问题。这就考虑自定义方案了。网上看到一篇可以把字符串的数字扣出来的方案。把中文转为数字,再进行排序。不多说,直接上代码
javabean 实现comparable 接口,在compareTo方法中实现排序。
bean
package cn.wq.elf.entity;
import android.os.Parcel;
import android.os.Parcelable;
import cn.wq.elf.utils.StringUtils;
public class BaseBookEntity implements Parcelable, Comparable<BaseBookEntity> {
private String bookName;
private String newName;
private String content;
private int viewType;
private boolean isAdd = false;
private boolean isEdit = false;
private long numTitle;
public BaseBookEntity() {
}
public BaseBookEntity(String bookName, int viewType) {
this.bookName = bookName;
this.viewType = viewType;
}
public BaseBookEntity(int viewType, boolean isAdd) {
this.viewType = viewType;
this.isAdd = isAdd;
}
public long getNumTitle() {
return numTitle;
}
public void setNumTitle(long numTitle) {
this.numTitle = numTitle;
}
public BaseBookEntity(String bookName, String content, int viewType) {
this.bookName = bookName;
this.content = content;
this.viewType = viewType;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getNewName() {
return newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getViewType() {
return viewType;
}
public void setViewType(int viewType) {
this.viewType = viewType;
}
public boolean isEdit() {
return isEdit;
}
public void setEdit(boolean edit) {
isEdit = edit;
}
public boolean isAdd() {
return isAdd;
}
public void setAdd(boolean add) {
isAdd = add;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.bookName);
dest.writeString(this.newName);
dest.writeString(this.content);
dest.writeInt(this.viewType);
dest.writeByte(this.isAdd ? (byte) 1 : (byte) 0);
dest.writeByte(this.isEdit ? (byte) 1 : (byte) 0);
}
protected BaseBookEntity(Parcel in) {
this.bookName = in.readString();
this.newName = in.readString();
this.content = in.readString();
this.viewType = in.readInt();
this.isAdd = in.readByte() != 0;
this.isEdit = in.readByte() != 0;
}
public static final Parcelable.Creator<BaseBookEntity> CREATOR = new Parcelable.Creator<BaseBookEntity>() {
@Override
public BaseBookEntity createFromParcel(Parcel source) {
return new BaseBookEntity(source);
}
@Override
public BaseBookEntity[] newArray(int size) {
return new BaseBookEntity[size];
}
};
@Override
public int compareTo(BaseBookEntity otheBook) {
this.setNumTitle(StringUtils.getIntTitle(this.getBookName()));
otheBook.setNumTitle(StringUtils.getIntTitle(otheBook.getBookName()));
return (int) (this.getNumTitle() - otheBook.getNumTitle());
}
}
Stringutils
package cn.wq.elf.utils;
import java.util.Stack;
public class StringUtils {
/**
* 比较文件名是否相等
*
* @param name1
* @param name2
* @return
*/
public static boolean isEquals(String name1, String name2) {
if (ToolEmpty.isNotEmpty(name1) && ToolEmpty.isNotEmpty(name2)) {
return name1.trim().replaceAll("\t", "").equals(name2.trim().replaceAll("\t", ""));
}
return false;
}
/**
* 截取第一处的中文数字字符串
*
* @param str
* @return
*/
public static String getNumberStr(String str) {
StringBuilder stringBuilder = new StringBuilder();
boolean isFirst = true;
String chineseNumStr = "零一二三四五六七八九十百千万亿";
for (int i = 0; i < str.length(); i++) {
String tempStr = str.substring(i, i + 1);
if (chineseNumStr.contains(tempStr)) {
stringBuilder.append(tempStr);
if (isFirst) {
isFirst = false;
}
} else {
if (!isFirst) {
break;
}
}
}
return stringBuilder.toString();
}
/**
* 将中文数字转换为 阿拉伯数字
*
* @param chineseNumber 中文数字字符串
* @return 转换后的阿拉伯数字
* @author Tuzi294
*/
public static long chineseNumber2Int(String chineseNumber) {
String aval = "零一二三四五六七八九";
String bval = "十百千万亿";
int[] bnum = {10, 100, 1000, 10000, 100000000};
long num = 0;
char[] arr = chineseNumber.toCharArray();
int len = arr.length;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < len; i++) {
char s = arr[i];
//跳过零
if (s == '零') continue;
//用下标找到对应数字
int index = bval.indexOf(s);
//如果不在bval中,即当前字符为数字,直接入栈
if (index == -1) {
stack.push(aval.indexOf(s));
} else { //当前字符为单位。
int tempsum = 0;
int val = bnum[index];
//如果栈为空则直接入栈
if (stack.isEmpty()) {
stack.push(val);
continue;
}
//如果栈中有比val小的元素则出栈,累加,乘N,再入栈
while (!stack.isEmpty() && stack.peek() < val) {
tempsum += stack.pop();
}
//判断是否经过乘法处理
if (tempsum == 0) {
stack.push(val);
} else {
stack.push(tempsum * val);
}
}
}
//计算最终的和
while (!stack.isEmpty()) {
num += stack.pop();
}
return num;
}
/**
* 将汉字转数字
*
* @param str
* @return
*/
public static long getIntTitle(String str) {
String numberStr = getNumberStr(str);
return chineseNumber2Int(numberStr);
}
}
测试类:
private ArrayList<BaseBookEntity> dataList = new ArrayList<>();
void sort(){
dataList.add(...)
dataList.add(...)
dataList.add(...)
Collections.sort(dataList);
}