首先需要排序的对象需要实现Comparable接口并且并复写compareto方法:
public class SysOffice extends DataEntity<SysOffice> implements Comparable<SysOffice>{
private static final long serialVersionUID = 1L; private String name; // 名称
public String getName() { return name; } public void setName(String name) { this.name = name; }
@Override public int compareTo(SysOffice o) { return this.name.compareTo(o.getName()); }
}
在controller中,获取到List<SysOffice>后做如下处理:
List<SysOffice> sysOfficeList = sysOfficeService.findList(sysOffice); //Collections工具类的sort()方法对list集合元素排序 Collections.sort(sysOfficeList,new Comparator<SysOffice>(){ @Override public int compare(SysOffice o1, SysOffice o2) { //获取中文环境 Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA); return com.compare(o1.getName(),o2.getName()); } });