c# sorting a List<> using Tuple?

前端 未结 3 750
半阙折子戏
半阙折子戏 2021-01-12 05:24

I need to sort a List<> of MediaItem objects by publish date...the publish date is not a property of the item. So my initial intention was to temporarily tack on a publi

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 05:58

    One way would be to use the List.Sort method, using a lambda-expression to create the appropriate Comparison delegate.

    // Sorts tuples in chronological order. To use reverse chronological order,
    // use t2.Item2.CompareTo(t1.Item2) instead.
    list.Sort((t1, t2) => t1.Item2.CompareTo(t2.Item2));
    

    This of course assumes that there are no null-references in the list, which there can't be in your example.

提交回复
热议问题