ArrayList里存放的对象有2个属性,一个Type1,一个Type2,想对整个ArrayList按Type1先排序,然后在Type1排序好的基础上再安Type2排序。比如说:排序前可能是这样的情况
Type1   Type2
--------------
  1       a
  2       c
  2       a
  1       b
  2       b希望排序后成为这样的情况
Type1   Type2
--------------
  1       a
  1       b
  2       a
  2       b
  2       c我现在只知道按Type1排序的方法,就是实现IComparer的那种,请问如何按2种条件排序?

解决方案 »

  1.   

    不能这样排序的。sort功能有限
      

  2.   

    呵呵,俺最近也在考虑类似滴问题,不过是在 List<T> 上实现多属性排序,
    可是手头的项目正在重构中,没工夫弄这个,先说说基本思路:1 按 Type1 正常排序;
    2 取出 Type1 唯一值,即不重复值,
    3 遍历 Type1 唯一值,从对排序后的列表中取出相应集合,再按 Type2 排序即可!
    4 收工!扩展,如果还有 Type3 排序,就重复 2、3 两步即可!嘿嘿,说着说着基本都实现了,俺明早起来去Coding出来!
      

  3.   

    用sort,排序比较函数自己写
      

  4.   

    可以的 ,先把Type1 为1的值读出来 在判断Type2 值小就放在前面。
     Type1 为2的值也一样。
    如果你有非常多的数据 , 就先把Type1值排序读出,然后循环Type1 取出值,然后排Type2
      

  5.   

    可以 用List来实现
     public class TempType
        {
            public int Type1 { get; set; }
            public string Type2 { get; set; }        public TempType(int type1, string type2)
            {
                Type1 = type1;
                Type2 = type2;
            }
        }            List<TempType> list = new List<TempType>();
                list.Add(new TempType(1, "a"));
                list.Add(new TempType(2, "c"));
                list.Add(new TempType(2, "a"));
                list.Add(new TempType(1, "b"));
                list.Add(new TempType(2, "b"));
                var a = from l in list
                        orderby l.Type1, l.Type2
                        select l;
      

  6.   

    arrayList.Cast<T>.Orderby(T.T1).Thenby(T.T2)
    如果你的类型是基础类型属性的话并且我的方法没拼错那就直接可以使用了O(∩_∩)O~
      

  7.   

        自己写比较函数。例子见我的个人网站。
    【签名】每个月总有那么几天,您的网络会受到黑客的攻击?坐立不安,烦躁无力,使用我和human开发的“月月舒”防火墙,超轻超薄,易于携带,提供由内到外的全方位保护,即使流量再大,也可以冲浪自如,再也不用担心侧漏啦。
    http://webtrados.008.net 网络小筑,常来坐坐。
      

  8.   

    http://xujiayou.w66.mydnns.cn/post/5.html
      

  9.   

    通过arrlyList的Sort(IComparer) 
    存放的对象类继承IComparer 接口
    实现Compare方法时先比较Type1,如果Type1相等继续比较Type2
    class Class1:IComparer
        {
            private int id;
            private string name;
            public  Class1()
            { 
            }        public Class1(int id, string name)
            {
                this.id = id;
                this.name = name;
            }        int IComparer.Compare(Object x, Object y)
            {
                Class1 tempx = x as Class1;
                Class1 tempy = y as Class1;
                int returnCode=-2;
                if (tempx != null && tempy != null)
                {
                    returnCode = tempx.id.CompareTo(tempy.id);
                    if (returnCode == 0)
                    {
                        returnCode = tempx.name.CompareTo(tempy.name);
                    }
                }            return returnCode;
            }
        }ArrayList arrlist = new ArrayList();
                arrlist.Add(new Class1(1, "a"));
                arrlist.Add(new Class1(2, "c"));
                arrlist.Add(new Class1(2, "a"));
                arrlist.Add(new Class1(1, "b"));
                arrlist.Add(new Class1(2, "b"));            IComparer myComparer = new Class1();
                arrlist.Sort(myComparer);
      

  10.   

    from l in list orderby l.Type1, l.Type2 select l;晕。抢我代码对,没错,用LINQ,前面的仁兄已经给了最好答案了。
      

  11.   

    受到 13 楼滴启发,按把原有滴 DataModelManager<T>.Sort(List<T> listOfT, string sortPropName)
    改为了 
    DataModelManager<T>.Sort(List<T> listOfT, List<SortInfo<T>> sortInfoList)SortInfo 是用于保存 排序属性名及升降序滴对象,用 List 来实现多属性同时排序;例如:
    DataModelManager<Student>.Sort(studentList, Student.School_Symbol, Student.Class_Symbol, Student.Name_Symbol);
    这是一个 Sort 的简单重载,将 studentList 按照 学生的 学校名、班级、姓名 排序。刚刚测试了,多属性排序工作正常!