需求
需要从产品分类中找出所有分类,根据英文名称首字母按照顺序排列
页面展示
编写一个类
InitialsComparator
实现
Comparator
代码如下
/** * <p>Description: 按照类目的英文首字母排序 </p> * <p>此模块分功能:Other开头的类目置于最后</p> * <p> 20200826</p> * * @author 李金泉 * @version 1.0 */ public class InitialsComparator implements Comparator<TjmItemgroup> { @Override public int compare(TjmItemgroup o1, TjmItemgroup o2) { if (o1.getJmIgEname() == null ) { return 1; } return o1.getJmIgEname().substring(0, 1).compareTo(o2.getJmIgEname().substring(0, 1)); } public static int convert(Integer tmp) { return (tmp == null) ? 0 : tmp; } }
用法
从数据库中获取到你需要的分类记录
List<TjmItemgroup> lst = xxxxxxxxxxxxxxx你获取记录的逻辑;
if(lst==null){ return; }
Collections.sort(lst, new InitialsComparator());