Sort method versus OrderBy LINQ extension method

I have a class Product with an Id and a Name as properties. There are multiple ways of getting a list of products to display in sorted/ordered fashion, say, by the Name of the product. The two I’m concerned about here are the Sort and the OrderBy extension method through LINQ and the difference between them.

   1: public class Product
   2: {
   3:     public int Id { get; set; }
   4:     public string Name { get; set; }
   5: }

Below is the list of products that I’ll be using and is defined somewhere in the Program.cs of my console application.

   1: private static List<Product> GetProducts()
   2: {
   3:     List<Product> products = new List<Product>
   4:                                  {
   5:                                      new Product {Id = 1, Name = "Camera"},
   6:                                      new Product {Id = 2, Name = "Nook"},
   7:                                      new Product {Id = 3, Name = "Mouse"},
   8:                                      new Product {Id = 4, Name = "Abacus"}
   9:                                  };
  10:     return products;
  11: }

Let’s start with the Sort method. Here’s my implementation of the IComparer<Product>.

   1: public class ProductComparer : IComparer<Product>
   2: {
   3:     public int Compare(Product x, Product y)
   4:     {
   5:         return x.Name.CompareTo(y.Name);
   6:     }
   7: }
Quite straight forward implementation there. Coming to the usage of this method, I can just do:
   1: private static void InPlaceSorting(List<Product> products)
   2: {
   3:     IComparer<Product> productComparer = new ProductComparer();
   4:     products.Sort(productComparer);
   5:  
   6:     foreach (Product product in products)
   7:     {
   8:         Console.WriteLine("{0} - {1}", product.Id, product.Name);
   9:     }
  10: }
  11: // call from my Main method:
  12: InPlaceSorting(GetProducts());

As expected, I get the output of:
4 – Abacus
1 – Camera
3 – Mouse
2 – Nook

Coming to the OrderBy extension method, I have my code as:

   1: private static void ReadingInParticularOrder(List<Product> products)
   2: {
   3:     foreach (Product product in products.OrderBy(p=>p.Name))
   4:     {
   5:         Console.WriteLine("{0} - {1}", product.Id, product.Name);
   6:     }
   7: }
   8: // call from my Main method:
   9: ReadingInParticularOrder(GetProducts());

I will again get the same output as before. So what’s the difference?

The actions ‘Sort’ and ‘OrderBy’ hold the clue. In the first instance, you are actually sorting the products collection – thereby changing the locations of the items in the collection. Whereas in the OrderBy, you are just reading the items of the collection in a particular order without changing the locations of the items in the products collection.

This becomes obvious, when I modify the second method ‘ReadingInParticularOrder’ to the following definition:

   1: private static void ReadingInParticularOrder(List<Product> products)
   2: {
   3:     foreach (Product product in products.OrderBy(p=>p.Name))
   4:     {
   5:         Console.WriteLine("{0} - {1}", product.Id, product.Name);
   6:     }
   7:  
   8:     Console.WriteLine("Original order");
   9:     foreach (Product product in products)
  10:     {
  11:         Console.WriteLine("{0} - {1}", product.Id, product.Name);
  12:     }
  13: }

Now my output will be:

4 – Abacus
1 – Camera
3 – Mouse
2 – Nook
Original order
1 – Camera
2 – Nook
3 – Mouse
4 – Abacus

So that’s the difference between Sorting a collection and Ordering by a particular ‘column’.

3 Comments

Comments have been disabled for this content.