雁过请留痕...
代码改变世界

实现List的多列排序

2011-05-20 12:15  xiashengwang  阅读(662)  评论(0编辑  收藏  举报

List的sort()方法,只能实现简单数据的排序,如过List里面装的是对象,就必须要显示实现IComparer接口,才能排序,并且有时我们需要对一个对象进行多列排序,如学生按学号,成绩升序排列。在实现这个接口的时候,我们可以对多个字段排序,代码如下:

        public class People
        {
            public string  Sex { get; set; }
            public int Age { get; set; }
            public string Name { get; set; }
        }
        public class PeopleComparer : IComparer<People>
        {
            private bool bASC;
            public PeopleComparer(bool asceding)
            {
                bASC = asceding;
            }

            #region IComparer<Student> 
            public int Compare(People x, People y)
            {
                if (!bASC)
                {
                    //降序
                    People temp;
                    temp = y;
                    y = x;
                    x = temp;
                }

                int flg = -1;
                flg = Comparer<string >.Default.Compare(x.Sex, y.Sex);
                if (flg == 0)
                {
                    flg = Comparer<int>.Default.Compare(x.Age, y.Age);
                }
                if (flg == 0)
                {
                    flg = Comparer<string>.Default.Compare(x.Name, y.Name);
                }
                return flg;
            }

            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<People> lstPeople = new List<People>();
            lstPeople.Add(new People{  Sex ="man", Age =40, Name ="name1"});
            lstPeople.Add(new People { Sex = "woman", Age = 15, Name = "name11" });
            lstPeople.Add(new People { Sex = "man", Age = 35, Name = "name4" });
            lstPeople.Add(new People { Sex = "man", Age = 55, Name = "name3" });
            lstPeople.Add(new People { Sex = "man", Age = 35, Name = "name5" });
            lstPeople.Add(new People { Sex = "man", Age = 60, Name = "name2" });
            lstPeople.Add(new People { Sex = "woman", Age = 27, Name = "name15" });
            lstPeople.Add(new People { Sex = "woman", Age = 37, Name = "name14" });
            lstPeople.Add(new People { Sex = "woman", Age = 27, Name = "name12" });
            lstPeople.Add(new People { Sex = "woman", Age = 47, Name = "name13" });

            System.Diagnostics.Debug.WriteLine("原始数据:");
            DispPeople(lstPeople);

            System.Diagnostics.Debug.WriteLine("升序排列:");
            PeopleComparer ascCompare = new PeopleComparer(true);
            lstPeople.Sort(ascCompare);
            DispPeople(lstPeople);

            System.Diagnostics.Debug.WriteLine("降序排列:");
            PeopleComparer descCompare = new PeopleComparer(false );
            lstPeople.Sort(descCompare);
            DispPeople(lstPeople);
        }

        private void DispPeople(IEnumerable<People> peoples)
        {
            foreach (People p in peoples)
            {
                System.Reflection.PropertyInfo[] pInfos = p.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    string str = "";
                    foreach (System.Reflection.PropertyInfo pi in pInfos)
                {
                    str += pi.Name + ":" + pi.GetValue(p, null).ToString().PadRight(10,' ');
                }
                    System.Diagnostics.Debug.WriteLine(str);
            }
        }

代码实现了按人的性别,年龄,名字排序,可以实现升序和降序。

结果:

原始数据:
Sex:man       Age:40        Name:name1     
Sex:woman     Age:15        Name:name11    
Sex:man       Age:35        Name:name4     
Sex:man       Age:55        Name:name3     
Sex:man       Age:35        Name:name5     
Sex:man       Age:60        Name:name2     
Sex:woman     Age:27        Name:name15    
Sex:woman     Age:37        Name:name14    
Sex:woman     Age:27        Name:name12    
Sex:woman     Age:47        Name:name13    
升序排列:
Sex:man       Age:35        Name:name4     
Sex:man       Age:35        Name:name5     
Sex:man       Age:40        Name:name1     
Sex:man       Age:55        Name:name3     
Sex:man       Age:60        Name:name2     
Sex:woman     Age:15        Name:name11    
Sex:woman     Age:27        Name:name12    
Sex:woman     Age:27        Name:name15    
Sex:woman     Age:37        Name:name14    
Sex:woman     Age:47        Name:name13    
降序排列:
Sex:woman     Age:47        Name:name13    
Sex:woman     Age:37        Name:name14    
Sex:woman     Age:27        Name:name15    
Sex:woman     Age:27        Name:name12    
Sex:woman     Age:15        Name:name11    
Sex:man       Age:60        Name:name2     
Sex:man       Age:55        Name:name3     
Sex:man       Age:40        Name:name1     
Sex:man       Age:35        Name:name5     
Sex:man       Age:35        Name:name4