如何在 .NET 4.0 中对并发集合进行排序

发布于 2024-11-28 20:33:20 字数 231 浏览 1 评论 0原文

如何在 .NET 4.0 中对并发集合进行排序 例如,我构建了我的 ConcurrentBag 集合。如何对其中的元素进行排序?

ConcurrentBag<string> stringCollection;

ConcurrentBag<CustomType> customCollection;

How to sort a concurrent collection in .NET 4.0
For example I have constructed my ConcurrentBag collection. How can I sort the elements in it?

ConcurrentBag<string> stringCollection;

ConcurrentBag<CustomType> customCollection;

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

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

发布评论

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

评论(4

梦开始←不甜 2024-12-05 20:33:20

为了扩展 DSW 的答案,您可以在可枚举上使用 OrderBy。

customCollection.OrderBy(cc => cc.FieldToOrderBy);

您也可以按降序执行:

customCollection.OrderByDescending(cc => cc.FieldToOrderBy);

To expand on DSW's answer, you can use the OrderBy on an enumerable.

customCollection.OrderBy(cc => cc.FieldToOrderBy);

You can also do it in descending order:

customCollection.OrderByDescending(cc => cc.FieldToOrderBy);
薄荷梦 2024-12-05 20:33:20

您可以使用 OrderBy 方法进行排序

,也可以尝试一下。

var result = stringCollection.AsParallel().AsOrdered();

有关详细信息,请查看下面的链接

http://msdn.microsoft.com/en-us/library/dd460719.aspx,您可以了解如何使用进行复杂排序PLINQ,例如:

 var q2 = orders.AsParallel()
       .Where(o => o.OrderDate < DateTime.Parse("07/04/1997"))
       .Select(o => o)
       .OrderBy(o => o.CustomerID) // Preserve original ordering for Take operation.
       .Take(20)
       .AsUnordered()  // Remove ordering constraint to make join faster.
       .Join(
              orderDetails.AsParallel(),
              ord => ord.OrderID,
              od => od.OrderID,
              (ord, od) =>
              new
              {
                  ID = ord.OrderID,
                  Customer = ord.CustomerID,
                  Product = od.ProductID
              }
             )
       .OrderBy(i => i.Product); // Apply new ordering to final result sequence.

you can use OrderBy method for sorting

and also try this too..

var result = stringCollection.AsParallel().AsOrdered();

for more information check below link

http://msdn.microsoft.com/en-us/library/dd460719.aspx, you can lean how to do complex sorting using PLINQ, e.g:

 var q2 = orders.AsParallel()
       .Where(o => o.OrderDate < DateTime.Parse("07/04/1997"))
       .Select(o => o)
       .OrderBy(o => o.CustomerID) // Preserve original ordering for Take operation.
       .Take(20)
       .AsUnordered()  // Remove ordering constraint to make join faster.
       .Join(
              orderDetails.AsParallel(),
              ord => ord.OrderID,
              od => od.OrderID,
              (ord, od) =>
              new
              {
                  ID = ord.OrderID,
                  Customer = ord.CustomerID,
                  Product = od.ProductID
              }
             )
       .OrderBy(i => i.Product); // Apply new ordering to final result sequence.
记忆之渊 2024-12-05 20:33:20

从集合中获取列表,对列表进行排序,例如:

ConcurrentBag<string> bag = new ConcurrentBag<string>();

var temp = bag.ToList();
temp.Sort();//you can apply a custom sort delegate here

bag = new ConcurrentBag<string>(temp);

Get a list from the collection, sort the list, like:

ConcurrentBag<string> bag = new ConcurrentBag<string>();

var temp = bag.ToList();
temp.Sort();//you can apply a custom sort delegate here

bag = new ConcurrentBag<string>(temp);
韬韬不绝 2024-12-05 20:33:20

您可以使用 PLINQ,也可以编写实现自己的并行排序函数,如本文中的函数 http://www.emadomara.com/2011/08/parallel-merge-sort-using-barrier.html

you can either use PLINQ or you can write implement your own parallel sort function like the one in this article http://www.emadomara.com/2011/08/parallel-merge-sort-using-barrier.html

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