Building Ordering a tree as a list in C#

前端 未结 3 1344
旧巷少年郎
旧巷少年郎 2021-01-14 18:05

I have a list of C# entities. My entity is defined as follows:

public class Item
{
    // the id of an item
    public Guid ID { get; set; }

    // if this          


        
3条回答
  •  独厮守ぢ
    2021-01-14 18:21

    Literally, to sort the list, you should use method called Sort. You just need to provide correct comparator, it means you need to define a method which will compare two items as you defined (by level, then createdate). Look at MSDN here: http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx

    For example you can use a lambda like this:

    mylist.Sort((x,y) => {
      int c = x.Level.CompareTo(y.Level);
      if(c==0) c = x.CreateDate.CompareTo(y.CreateDate);
      return c;
    });
    

    Note that I didn't compile and test this code in Visual Studio. Maybe it isn't 100% right but hopefully it showed you where to start.

提交回复
热议问题