package com.msdw.tms; import com.msdw.tms.entity.ProjectEntity; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List project = new ArrayList<>(); ProjectEntity p1 = new ProjectEntity(); p1.setProjectid(1); p1.setProjectname("1"); project.add(p1); ProjectEntity p2 = new ProjectEntity(); p2.setProjectid(3); p2.setProjectname("3"); project.add(p2); ProjectEntity p3 = new ProjectEntity(); p3.setProjectid(2); p3.setProjectname("2"); project.add(p3); project.sort(new Comparator() { @Override public int compare(ProjectEntity o1, ProjectEntity o2) { if(StringUtils.isEmpty(o1.getProjectid()) || StringUtils.isEmpty(o2.getProjectid()) ) return 0; return o1.getProjectid().compareTo(o2.getProjectid()); } }); for (ProjectEntity pro:project) { System.out.println(pro); } } }