How to sort a parallel collection in .NET 4.0

How to sort a parallel collection in .NET 4.0 For example, I created my collection ConcurrentBag. How can I sort the items in it?

ConcurrentBag<string> stringCollection;

ConcurrentBag<CustomType> customCollection;
+5
source share
4 answers

To extend the answer to DSW, you can use OrderBy for listing.

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

You can also do this in descending order:

customCollection.OrderByDescending(cc => cc.FieldToOrderBy);
+5
source

you can use the method OrderByto sort

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, , , 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.
+2

Get the list from the collection, sort the list, for example:

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);
0
source

you can use PLINQ, or you can write to implement your own parallel sorting function, such as in this article http://www.emadomara.com/2011/08/parallel-merge-sort-using-barrier.html

0
source

All Articles