How can I sort List based on properties of T?

后端 未结 4 894
误落风尘
误落风尘 2020-12-28 18:21

My Code looks like this :

Collection optionInfoCollection = ....
List optionInfoList = new List

        
4条回答
  •  攒了一身酷
    2020-12-28 18:58

    public class Person  {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    List people = new List();
    
    people.Sort(
        delegate(Person x, Person y) {
            if (x == null) {
                if (y == null) { return 0; }
                return -1;
            }
            if (y == null) { return 0; }
            return x.FirstName.CompareTo(y.FirstName);
        }
    );
    

提交回复
热议问题