public class ComparatorLevel implements Comparator<LabelLevel> {
/**
* 复写比较器--根据一级编码比较,相同根据二级比较,相同根据三级比较,相同 根据四级比较
* {@inheritDoc}
*/
@Override
public int compare(LabelLevel o1, LabelLevel o2) {
int flag1 = o1.getOneCode().compareTo(o2.getOneCode());
if (flag1 != 0) {
return flag1;
}
if (StringUtils.isNotEmpty(o1.getTwoCode()) && StringUtils.isNotEmpty(o2.getTwoCode())) {
flag1 = o1.getTwoCode().compareTo(o2.getTwoCode());
if (flag1 != 0) {
return flag1;
}
} else {
return flag1;
}
if (StringUtils.isNotEmpty(o1.getThreeCode()) && StringUtils.isNotEmpty(o2.getThreeCode())) {
flag1 = o1.getThreeCode().compareTo(o2.getThreeCode());
if (flag1 != 0) {
return flag1;
}
} else {
return flag1;
}
if (StringUtils.isNotEmpty(o1.getFourCode()) && StringUtils.isNotEmpty(o2.getFourCode())) {
flag1 = o1.getFourCode().compareTo(o2.getFourCode());
if (flag1 != 0) {
return flag1;
}
} else {
return flag1;
}
return flag1;
}
/**
* 复写比较器--根据一级编码比较,相同根据二级比较,相同根据三级比较,相同 根据四级比较
* {@inheritDoc}
*/
@Override
public int compare(LabelLevel o1, LabelLevel o2) {
int flag1 = o1.getOneCode().compareTo(o2.getOneCode());
if (flag1 != 0) {
return flag1;
}
if (StringUtils.isNotEmpty(o1.getTwoCode()) && StringUtils.isNotEmpty(o2.getTwoCode())) {
flag1 = o1.getTwoCode().compareTo(o2.getTwoCode());
if (flag1 != 0) {
return flag1;
}
} else {
return flag1;
}
if (StringUtils.isNotEmpty(o1.getThreeCode()) && StringUtils.isNotEmpty(o2.getThreeCode())) {
flag1 = o1.getThreeCode().compareTo(o2.getThreeCode());
if (flag1 != 0) {
return flag1;
}
} else {
return flag1;
}
if (StringUtils.isNotEmpty(o1.getFourCode()) && StringUtils.isNotEmpty(o2.getFourCode())) {
flag1 = o1.getFourCode().compareTo(o2.getFourCode());
if (flag1 != 0) {
return flag1;
}
} else {
return flag1;
}
return flag1;
}
}
使用:
//根据层级编码进行排序
List<LabelLevel> subList = new ArrayList<LabelLevel>();
ComparatorLevel comparator = new ComparatorLevel();
Collections.sort(subList, comparator);