Utility of List.Sort() versus List.OrderBy() for a member of a custom container class

前端 未结 2 2005
暖寄归人
暖寄归人 2021-02-06 04:03

I\'ve found myself running back through some old 3.5 framework legacy code, and found some points where there are a whole bunch of lists and dictionaries that must be updated in

2条回答
  •  遥遥无期
    2021-02-06 04:32

    As Julien said, Sort() will probably be faster, however since you mentioned the better readability and flexibility of OrderBy(), you can achieve that with your Sort() method, too (at least if the Property you want to base your sort on is comparable)

    items = items.OrderBy(x => x.Name).ToList();
    items.Sort((x,y) => x.Name.CompareTo(y.Name)); // If x.Name is never null
    items.Sort((x,y) => String.Compare(x.Name, y.Name)); // null safe
    

提交回复
热议问题