两个对象的大致代码为
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属性进行排序
哪位高人帮帮忙!最好有代码,谢谢了!

解决方案 »

  1.   

    用TreeSet
    CacheAction 实现 Comparator接口,就好了
      

  2.   

    看我的博客: http://blog.csdn.net/numen_wlm/archive/2007/08/22/1753802.aspx
      

  3.   

    CacheAction实现Comparable接口,实现具体的方法Comparator方法时用DicData,在DicData中也实现了Comparable接口,用order排序!然后就用TreeSet就好了!
      

  4.   

    这个set用的是LinkedHashSet类
    Set<CacheAction> actions = new LinkedHashSet<CacheAction>();
    各位大哥有没有代码呀?
      

  5.   

    numen_wlm你写的太复杂了,看不懂呀,我是问有没有简单点的呀?
      

  6.   

    CacheAction 实现 Comparator接口
      

  7.   

    建议改用TreeSetCacheAction 实现Comparable接口
    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吧
      

  8.   

    给你个例子,不知道符合你的要求不
    是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,原因你应该明白
      

  9.   

    SET是无序的吧。
    要排序只能用SortedSet的实现类啊
      

  10.   


    升序:
    Collections.sort(caSet, new Comparator<CacheAction>(){
        public int compare(CacheAction ca1,CacheAction ca2){
            return ca1.getDicId().getOrder()<ca1.getDicId().getOrder();
        }
    });
    降序:把“<”换成“>”;试试这个
      

  11.   

    谢谢各位,问题已经解决了,我是这样做的:
    把要比较的Set集合中的对象放到一个list中,把对象CacheAction对象实现CompareAble接口
    然后把list使用Collections.sort(list)排序就搞定了,没想到比想象中简单多了,呵呵