有一个对象List<Student> stuList,  student 有两个成员,id ,name 默认情况  stuList 是以id 排序的 .我怎样才能让他以name 排序呢?
比如:stuList 中默认 是
1 AAA
2 CCC
3 BBB
我怎么让它这样排序
1 AAA
3 BBB
2 CCC
谢谢大家

解决方案 »

  1.   

    需要自己实现IComparable借口
        class Program
        {
            public static void Main(string[] args)
            {
                List<Student> student = new List<Student>();
                Random rd = new Random();
                for (int i = 0; i < 60; i++)
                {
                    student.Add( new Student(rd.Next()
                        , ((char)('a' + rd.Next(0, 24))).ToString()));
                }            
                student.Sort();
                foreach (Student s in student)
                {
                    Console.WriteLine("ID:{0}\tNAME:{1}",s.id, s.Name);
                }
            }
        }
        class Student : IComparable//实现IComparable接口
        {
            public string Name;
            public int id;        public int CompareTo(object _obj2)
            {
                return id - ((Student)_obj2).id;
            }
            public Student(int i, string n)
            {
                Name = n;
                id = i;
            }
        }
      

  2.   

    严重同意楼上的
    要sort 就要实现IComparable这个接口···
      

  3.   

    public class ComparerEntity: IComparer<Area>{
        public int Compare(Area area1, Area area2)
        {
            if (string.Compare(area1.AreaName, area2.AreaName) > 0)
            {
                return 1;
            }
            if (string.Compare(area1.AreaName, area2.AreaName) == 0)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
    }
    搞定了
    我用了List.Sort(IComparer<T> comparer)  
     也可以实现想要的功能了。 谢谢大家
      

  4.   

    http://community.csdn.net/Expert/topic/5076/5076789.xml?temp=.9676782