时间:2019-03-07 标签:c#listsorting<>使用元组?

发布于 2024-11-03 06:44:10 字数 617 浏览 0 评论 0 原文

我需要对 List<> 进行排序按发布日期排列的 MediaItem 对象...发布日期不是项目的属性。所以我最初的意图是暂时附加一个发布日期属性,加载它们,排序,然后放弃该属性。我工作中的某人建议我使用 Tuple 并使用 LINQ 进行排序...我已经在我的代码片段测试中完成了这一点,填充了列表<>...但我不确定如何按该日期属性进行排序使用 LINQ。我会继续寻找,但如果有人能提供任何帮助,我将不胜感激。谢谢。

List<Tuple<MediaItem, DateTime>> list = new List<Tuple<MediaItem, DateTime>>();

            for (int i = 0; i <= 10; i++)
            {
                DateTime date = new DateTime(2011, i, 1);
                MediaItem item = new MediaItem();
                list.Add(new Tuple<MediaItem, DateTime>(item, date));
            }

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 publish date property, load em up, sort, and ditch the property. Someone at my work suggested I use Tuple and sort with LINQ...I've got about this far in my snippet test, filling up the List<>...but I'm not sure how to sort by that date property using LINQ. I'll keep looking on my end but if anyone's got any help I'd appreciate it. Thanks.

List<Tuple<MediaItem, DateTime>> list = new List<Tuple<MediaItem, DateTime>>();

            for (int i = 0; i <= 10; i++)
            {
                DateTime date = new DateTime(2011, i, 1);
                MediaItem item = new MediaItem();
                list.Add(new Tuple<MediaItem, DateTime>(item, date));
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走就滚别墨迹 2024-11-10 06:44:10

您可以使用 OrderBy( ) LINQ 运算符来执行排序;它允许您传递一个函数来提取要排序的元素。由于元组的第二个成员是日期,因此我们按 Item2 排序。

var result = list.OrderBy( x => x.Item2 ).ToList();

您可以使用 OrderByDescending() 而不是 `OrderBy() 来反转 LINQ 中的排序。

另请注意,您必须具体化结果或迭代它们,因为默认情况下 OrderBy() 方法是惰性的。上面的示例具体化了列表的副本。

如果您想就地对列表进行排序(而不是创建新列表),您可以向 Comparison 委托/en-us/library/w56d4y5z.aspx" rel="noreferrer">Sort() 方法。

 list.Sort( (a,b) => a.Item2.CompareTo(b.Item2) );

但是,您可以做得更好 - 如果您始终希望按排序顺序维护列表,则可以使用 SortedList 类,但是在这种情况下,您必须实现自定义 IComparer> 。在这种情况下,当您添加项目时,它们将始终按排序顺序维护。

You can use the OrderBy( ) LINQ operator to perform the sorting; it allows you to pass a function which extracts the element to sort by. Since the second member of the tuple is the date, we order by Item2.

var result = list.OrderBy( x => x.Item2 ).ToList();

You can reverse the ordering in LINQ by using OrderByDescending() instead of `OrderBy().

Also, note that you must either materialize the results or iterate over them, as the OrderBy() method is lazy by default. The example above materializes a copy of the list.

If you want to sort the list in place (rather than create a new one), you can supply a Comparison<T> delegate to the Sort() method.

 list.Sort( (a,b) => a.Item2.CompareTo(b.Item2) );

However, you can do even better - if you always want to main the list in sorted order, you can use the SortedList class instead ... however you will have to implement a custom IComparer<Tuple<MediaItem, DateTime>> in that case. In this case, when you add items they will always be maintained in sorted order.

吻风 2024-11-10 06:44:10

一种方法是使用 List.Sort 方法,使用 lambda 表达式创建适当的 Comparison 委托。

// 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));

当然,这假设列表中没有空引用,而您的示例中不可能有空引用。

One way would be to use the List<T>.Sort method, using a lambda-expression to create the appropriate Comparison<T> 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.

甜嗑 2024-11-10 06:44:10
List<Tuple<MediaItem, DateTime>> list = new List<Tuple<MediaItem, DateTime>>();

 for (int i = 0; i <= 10; i++)
 {
      DateTime date = new DateTime(2011, i, 1);
      MediaItem item = new MediaItem();
      list.Add(new Tuple<MediaItem, DateTime>(item, date));
 }

list.Sort((a, b) => a.Item2.CompareTo(b.Item2));

foreach (var element in list)
{
    Console.WriteLine(element);
}
List<Tuple<MediaItem, DateTime>> list = new List<Tuple<MediaItem, DateTime>>();

 for (int i = 0; i <= 10; i++)
 {
      DateTime date = new DateTime(2011, i, 1);
      MediaItem item = new MediaItem();
      list.Add(new Tuple<MediaItem, DateTime>(item, date));
 }

list.Sort((a, b) => a.Item2.CompareTo(b.Item2));

foreach (var element in list)
{
    Console.WriteLine(element);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文