标签:
这里是一个类中类去实现条件优先排序的问题
package com.sun; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo { public Demo() { // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a= 0; Dob db = new Dob(); Dob db1 = new Dob(); Dob db2 = new Dob(); db.setName("sun"); db.setAge(20); db.setSex(3); db1.setName("zhi"); db1.setAge(22); db1.setSex(1); db2.setName("yan"); db2.setAge(21); db2.setSex(2); List<Dob> as = new ArrayList<Dob>(); System.out.println("as="+as.size()); as.add(db); as.add(db1); as.add(db2); Collections.sort(as, new Comparator<Dob>() { //比较。如果年龄不同先比较年龄 ,相同就比较性别 public int compare(Dob arg0, Dob arg1) { int flg = arg0.getAge().compareTo(arg1.getAge()); if(flg==0){ return arg0.getSex().compareTo(arg1.getSex()); }else{ return arg0.getAge().compareTo(arg1.getAge()); } } }); for (Dob p : as) { System.out.println(p.getName()); System.out.println(p.getAge()); } System.out.println("as="+as.size()); } } class Dob { public String name; public Integer age; public Integer sex; 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 Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } }
标签:
原文地址:http://www.cnblogs.com/sunxun/p/4464226.html