package collection.list; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import testforstring.stringsomemonth; public class listCustomSort { public static void main(String[] args) { //一、数字、英文排序(保证list集合只有数字或者英文一种情况) List li = new ArrayList(); li.add("1"); li.add("email"); li.add("2"); li.add("comparator"); li.add("admin"); // li.add(3); // li.add(2); // li.add(1); Collections.sort(li); System.out.println("升序:"+li); Collections.reverse(li); System.out.println("降序:"+li); System.out.println("=================================================="); //二、中文排序:(中文:阿拉伯最前、其次拼音、再次汉字) List<String> li2 = new ArrayList<String>(); li2.add("梁朝伟"); li2.add("周星驰"); li2.add("黎明"); li2.add("张学友"); li2.add("Eason"); li2.add("5566"); Comparator<Object> Chinacompa = Collator.getInstance(java.util.Locale.CHINA); Collections.sort(li2, Chinacompa); System.out.println(li2); System.out.println("=================================================="); //三、实体类排序 List<Applicant> userlist = new ArrayList<Applicant>(); userlist.add(new Applicant("Eason(e)",33,"本科",8.5)); userlist.add(new Applicant("周星驰(z)",47,"大专",11.8)); userlist.add(new Applicant("黎姿(l)",28,"本科",8.5)); userlist.add(new Applicant("吴孟达(w)",28,"大专以下",9.7)); Comparator<Applicant> cmp = new ComparatorUser(); Collections.sort(userlist, cmp); for (Applicant user : userlist) { System.out.println(user.getName()); } //mian foot } } class ComparatorUser implements Comparator<Applicant> { @Override public int compare(Applicant u1, Applicant u2) { Integer u1xl = xlComparator(u1.getXl()); Integer u2xl = xlComparator(u2.getXl()); //1、先按学历排序 int flag = u1xl.compareTo(u2xl); //2、学历相同比较经验 if (flag==0) { flag=u1.getGzjy().compareTo(u2.getGzjy()); //3、经验相同比较年龄 if (flag==0) { flag=u1.getAge().compareTo(u2.getAge()); if (flag==0) { return u1.getName().compareTo(u2.getName()); } else { return flag; } } else { return flag; } } else { return flag; } } public Integer xlComparator(String xl){ int flag = 0; if ("本科".equals(xl)) { flag = 0; } else if("大专".equals(xl)){ flag = 1; } else if("大专以下".equals(xl)){ flag = 2; } return flag; } } class Applicant { private String name; private Integer age; private String xl; private Double gzjy; public Applicant() { super(); } public Applicant(String name, Integer age,String xl,Double gzjy) { super(); this.name = name; this.age = age; this.xl = xl; this.gzjy = gzjy; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getXl() { return xl; } public void setXl(String xl) { this.xl = xl; } public Double getGzjy() { return gzjy; } public void setGzjy(Double gzjy) { this.gzjy = gzjy; } }