集合合成的话
List<Article> list1 = new ArrayList<Article>();
/*.....添加数据.......*/List<Article> list2 = new ArrayList<Article>(); /*.....添加数据.......*/list1.addAll(list2);
一、被比较对象需实现Comparable接口中的compareTo方法
/*** 根据order对User排序*/public class User implements Comparable{ private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } public int compareTo(User arg0) { return this.getOrder().compareTo(arg0.getOrder()); }}
测试代码
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); Listlist = new ArrayList (); //此处add user2再add user1 list.add(user2); list.add(user1); for(User u : list){ System.out.println(u.getName()); } Collections.sort(list); for(User u : list){ System.out.println(u.getName()); } }}
输出结果
b
a
a
b
二、自定义一个比较器Comparator实现compare方法(这里采用匿名内部类的形式)
/*** 根据order对User排序*/public class User {//此处无需实现Comparable接口 private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } }
传入比较器,测试代码
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); Listlist = new ArrayList (); list.add(user2); list.add(user1); Collections.sort(list,new Comparator (){ public int compare(User arg0, User arg1) { return arg0.getOrder().compareTo(arg1.getOrder()); } }); for(User u : list){ System.out.println(u.getName()); } }}
三、多属性比较
添加一个id属性
/*** 多属性排序*/public class User { private Integer id; private String name; private Integer order; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } }
测试代码
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setId(1); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setId(2); user2.setName("a"); user2.setOrder(1); Listlist = new ArrayList (); list.add(user2); list.add(user1); Collections.sort(list,new Comparator (){ public int compare(User arg0, User arg1) { //第一次比较order int i = arg0.getOrder().compareTo(arg1.getOrder()); //如果order相同则比较name if(i==0){ int j= arg0.getName().compareTo(arg1.getName()); //如果name相同则比较id if (j==0){ return arg0.getId().compareTo(arg1.getId()); } return j; } return i; } }); for(User u : list){ System.out.println(u.getId()); } }}
参考文章: