两个对象的大致代码为
public class CacheAction {
private String id;//编号
private Cache cache;//共享人员
private DicData dicId;//动作
private String allow;//
。
}
public class DicData extends BaseObj
{ private String attrName; private Integer order;
}
现在有一个包含CacheAction对象的Set集合,我想按照CacheAction对象中属性DicData对象的order属性进行排序
哪位高人帮帮忙!最好有代码,谢谢了!
public class CacheAction {
private String id;//编号
private Cache cache;//共享人员
private DicData dicId;//动作
private String allow;//
。
}
public class DicData extends BaseObj
{ private String attrName; private Integer order;
}
现在有一个包含CacheAction对象的Set集合,我想按照CacheAction对象中属性DicData对象的order属性进行排序
哪位高人帮帮忙!最好有代码,谢谢了!
CacheAction 实现 Comparator接口,就好了
Set<CacheAction> actions = new LinkedHashSet<CacheAction>();
各位大哥有没有代码呀?
public class CacheAction implements Comparable<CacheAction>重写比较方法
public int compareTo(CacheAction arg0) {
if(getDicId().getOrder()>arg0.getDicId().getOrder()){
return 1;
}else if(getDicId().getOrder()<arg0.getDicId().getOrder()){
return -1;
}
return 0;
}因为TreeSet实现了SortedSet,自动排序,如果不能轻易修改Set的实现,考虑遍历赋值到一个TreeSet吧
是apache下的项目,随意排序,相关jar:commons-beanutils-1.6.jar、commons-collections-2.1.1.jar、commons-logging-1.0.4.jarpackage test;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;public class OrderTest { public static void main(String[] args) {
List<TestBean> list = new ArrayList<TestBean>();
for (int i = 0; i < 10; i++) {
TestOrderByBean o = new TestOrderByBean();
int id = i + 1;
o.setId(id);
o.setName("Name_" + id);
TestBean t = new TestBean();
t.setOrderby(o);
t.setDescription("Description_" + id);
list.add(t);
}
for (int i = 99; i >= 90; i--) {
TestOrderByBean o = new TestOrderByBean();
int id = i + 1;
o.setId(id);
o.setName("Name_" + id);
TestBean t = new TestBean();
t.setOrderby(o);
t.setDescription("Description_" + id);
list.add(t);
}
// 上面的排序遇到属性为null就会抛出异常, 也不能设定升序还是降序。
// 不过,可以借助commons-collections包的ComparatorUtils
// BeanComparator,ComparableComparator和ComparatorChain都是实现了Comparator这个接口
Comparator<TestBean> mycmp = ComparableComparator.getInstance();
mycmp = ComparatorUtils.nullLowComparator(mycmp); // 允许null
//默认升序
//mycmp = ComparatorUtils.reversedComparator(mycmp); // 逆序
Comparator cmp = new BeanComparator("orderby.id", mycmp);
Collections.sort(list, cmp);
for (TestBean t : list) {
System.out.println(t);
}
}
}
package test;public class TestBean {
private TestOrderByBean orderby; private String description; public TestOrderByBean getOrderby() {
return orderby;
} public void setOrderby(TestOrderByBean orderby) {
this.orderby = orderby;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public String toString() {
return "id=" + orderby.getId()+",name=" + orderby.getName() + ",description=" + description;
}
}package test;
public class TestOrderByBean {
private int id; private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}}以上这几个Bean的class必须为public,原因你应该明白
要排序只能用SortedSet的实现类啊
升序:
Collections.sort(caSet, new Comparator<CacheAction>(){
public int compare(CacheAction ca1,CacheAction ca2){
return ca1.getDicId().getOrder()<ca1.getDicId().getOrder();
}
});
降序:把“<”换成“>”;试试这个
把要比较的Set集合中的对象放到一个list中,把对象CacheAction对象实现CompareAble接口
然后把list使用Collections.sort(list)排序就搞定了,没想到比想象中简单多了,呵呵