思路:使用名字的拼音按顺序排列,addText 需要两个参数,一个是内容,一个是内容的拼音。里面会根据拼音来排序
哥们点个赞再走呀
制作不容易,条件允许情况下,打赏一下,谢谢
使用方法
Text text = new Text();
text.addText("谢霆锋","xietingfeng");
text.addText("周润发","zhourunfa");
text.addText("张家辉","zhangjiahui");
text.addText("刘德华","liudehua");
text.addText("张学友","zhangxueyou");
text.addText("杨幂","yangmi");
text.addText("张柏芝","zhangbozhi");
text.addText("陈冠希","chenguanxi");
text.addText("Alibaba","Alibaba");
text.addText("alibaba","alibaba");
text.addText("alibaba","Alibaba");
text.addText("百度","baidu");
text.addText("baidu","baidu");
List<String> text1 = text.toText();
System.out.println(text1);
核心代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Text {
private List<TextInfo> textList;
public Text() {
this(new ArrayList<TextInfo>());
}
public Text(List<TextInfo> textList) {
this.textList = textList;
}
public void sort(){
Collections.sort(textList, new Comparator<TextInfo>() {
@Override
public int compare(TextInfo o1, TextInfo o2) {
if (o1.textCode.equals(o2.textCode)){
return o1.text.compareTo(o2.text);
}
return o1.getTextCode().compareTo(o2.getTextCode());
}
});
}
public List<String> toText() {
sort();
List<String> list = new ArrayList<>();
for (int i = 0; i < textList.size(); i++) {
list.add(textList.get(i).toString());
}
return list;
}
public List<TextInfo> getTextList() {
sort();
return textList;
}
public void addText(String text, String textCode){
textList.add(new TextInfo(text,textCode));
}
public static class TextInfo {
private String text;
private String textCode;
public TextInfo(String text, String textCode) {
this.text = text;
this.textCode = textCode;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTextCode() {
return textCode;
}
public void setTextCode(String textCode) {
this.textCode = textCode;
}
public String getFirst(){
return String.valueOf(textCode.charAt(0)).toUpperCase();
}
@Override
public String toString() {
return getFirst()+":"+text+":"+textCode;
}
}
}