[code]package com.hwy.test;
import java.util.*;
public
class
ChapterSortTest {
public
static
void main(String[] args) {
List<String> chapterList = getDataFromDB();
Map<String,String> chapterMap = listChangeToMap(chapterList);
List<String> chapterNum = getChapterNum(chapterMap);
Map<Integer,String> chapterNumNoDot = removeDot(chapterNum);
int maxLength = getChapterNumMaxLength(chapterNumNoDot.keySet());
List<String> fillZeroChapterNum = fillZero(maxLength,chapterNumNoDot);
Collections.sort(fillZeroChapterNum);
List<String> sortChapterList = getSortChapterMap(fillZeroChapterNum,chapterMap,chapterNumNoDot);
if
(sortChapterList != null){
for
(String key:sortChapterList){
System.out.println(key);
}
}
}
public
static
List<String> getSortChapterMap(List<String> fillZeroChapterNum,Map<String,String> chapterMap,Map<Integer,String> chapterNotDot){
if
(null == fillZeroChapterNum || fillZeroChapterNum.size() == 0)
return
null;
if
(null == chapterMap)
return
null;
List<String> sortChapterList =
new
ArrayList<>();
for
(String temp:fillZeroChapterNum){
sortChapterList.add(chapterNotDot.get(Integer.parseInt(temp.replace(
"0"
,
""
))) +
" "
+ chapterMap.get(chapterNotDot.get(Integer.parseInt(temp.replace(
"0"
,
""
)))));
}
return
sortChapterList;
}
public
static
List<String> fillZero(int maxLength,Map<Integer,String> chapterNumNoDot){
if
(null == chapterNumNoDot || chapterNumNoDot.size() ==0)
return
null;
List<String> fillZeroList =
new
ArrayList<>();
for
(Integer key:chapterNumNoDot.keySet()){
fillZeroList.add(key + getNeedZero(maxLength - (key +
""
).length()));
}
return
fillZeroList;
}
public
static
String getNeedZero(int num){
if
(num <1)
return
""
;
StringBuffer sb =
new
StringBuffer();
for
(int i=0;i<num;i++){
sb.append(
"0"
);
}
return
sb.toString();
}
public
static
int max(int[] a){
int x;
int aa[]=
new
int[a.length];
System.arraycopy(a,0,aa,0,a.length);
x=aa[0];
for
(int i=1;i<aa.length;i++){
if
(aa[i]>x){
x=aa[i];
}
}
return
x;
}
public
static
int getChapterNumMaxLength(Set<Integer> chapterNumNoDot){
if
(null == chapterNumNoDot || chapterNumNoDot.size() == 0)
return
0;
Object[] chapterNumArr = chapterNumNoDot.toArray();
int[] chapterNum =
new
int[chapterNumArr.length];
for
(int i=0;i<chapterNumArr.length;i++){
chapterNum[i] = chapterNumArr[i].toString().length();
}
return
max(chapterNum);
}
public
static
Map<Integer,String> removeDot(List<String> chapterNumList){
if
(null == chapterNumList || chapterNumList.size() == 0)
return
null;
Map<Integer,String> rmDotChapterNumMap =
new
HashMap<>();
for
(int i=0;i<chapterNumList.size();i++){
rmDotChapterNumMap.put(Integer.parseInt(chapterNumList.get(i).replace(
"."
,
""
)),chapterNumList.get(i));
}
return
rmDotChapterNumMap;
}
public
static
List<String> getChapterNum(Map<String,String> chapterMap){
if
(null == chapterMap)
return
null;
List<String> chapterNumList =
new
ArrayList<>();
for
(String chapterNum:chapterMap.keySet()){
chapterNumList.add(chapterNum);
}
return
chapterNumList;
}
public
static
Map<String,String> listChangeToMap(List<String> chapterList){
Map<String,String> chapterMap =
new
HashMap<>();
if
(null == chapterList || chapterList.size() == 0)
return
null;
for
(String chapter:chapterList){
chapterMap.put(chapter.split(
" "
)[0], chapter.split(
" "
)[1]);
}
return
chapterMap;
}
public
static
List<String> getDataFromDB(){
List<String> chapterList =
new
ArrayList<>();
chapterList.add(
"1.3.1 华丽新设计"
);
chapterList.add(
"1.4 思想流派"
);
chapterList.add(
"3.1 短小"
);
chapterList.add(
"3.2 只做一件事"
);
chapterList.add(
"2.11 别伴可爱"
);
chapterList.add(
"4.4.12 注释掉的代码"
);
chapterList.add(
"1.1 要有代码"
);
chapterList.add(
"2.1 介绍"
);
chapterList.add(
"8.5 使用尚不存在的代码"
);
chapterList.add(
"5.3.1 水平方向上的区隔与靠近"
);
return
chapterList;
}
}