Suppose you create a Box class with some properties.

  1.  
  2. class Box
  3. {
  4.    public string Name
  5.    { get; set; }
  6.  
  7.    public string Color
  8.    { get; set; }
  9.  
  10.    public string Label
  11.    { get; set; }
  12.  
  13.    public DateTime CreatedAt
  14.    { get; set; }
  15.  
  16.    public override string ToString()
  17.    {
  18.      return string.Format("[Name: {0}; Color: {1}; Label: {2}; CreatedAt: {3}]",
  19.                           Name, Color, Label, CreatedAt);
  20.    }
  21. }


Now you made some boxes and want to sort these boxes according its properties.

With LINQ this is quit easily done.

  1.  
  2. class Program
  3. {
  4.   static void Main(string[] args)
  5.   {
  6.     IEnumerable<Box> boxes = new List<Box>() {
  7.       new Box() { Name = "Bananas", Color = "Yellow", Label = "z",
  8.                   CreatedAt = new DateTime(2010, 12, 9)},
  9.       new Box() { Name = "Pears", Color = "Green", Label = "x",
  10.                   CreatedAt = new DateTime(2008, 05, 7)},
  11.       new Box() { Name = "Apples", Color = "Red", Label = "y",
  12.                   CreatedAt = new DateTime(2010, 07, 1)},
  13.       };
  14.  
  15.     Console.WriteLine("Unsorted list");
  16.     LoopList(boxes);
  17.  
  18.     Console.WriteLine("\nList sorted on Name");
  19.     boxes = boxes.Select(item => item).OrderBy(item => item.Name);
  20.     LoopList(boxes);
  21.  
  22.     Console.WriteLine("\nList sorted on Color");
  23.     boxes = boxes.Select(item => item).OrderBy(item => item.Color);
  24.     LoopList(boxes);
  25.  
  26.     Console.WriteLine("\nList sorted on Label");
  27.     boxes = boxes.Select(item => item).OrderBy(item => item.Label);
  28.     LoopList(boxes);
  29.  
  30.     Console.WriteLine("\nList sorted on CreationDate");
  31.     boxes = boxes.Select(item => item).OrderBy(item => item.CreatedAt);
  32.     LoopList(boxes);
  33.  
  34.     Console.ReadKey();
  35.   }
  36.  
  37.   static void LoopList(IEnumerable<Box> boxes)
  38.   {
  39.      foreach (var item in boxes)
  40.      {
  41.         Console.WriteLine(item);
  42.      }
  43.   }
  44. }
  1.  
  2. Unsorted list
  3. [Name: Bananas; Color: Yellow; Label: z; CreatedAt: 9/12/2010 0:00:00]
  4. [Name: Pears; Color: Green; Label: x; CreatedAt: 7/05/2008 0:00:00]
  5. [Name: Apples; Color: Red; Label: y; CreatedAt: 1/07/2010 0:00:00]
  6.  
  7. List sorted on Name
  8. [Name: Apples; Color: Red; Label: y; CreatedAt: 1/07/2010 0:00:00]
  9. [Name: Bananas; Color: Yellow; Label: z; CreatedAt: 9/12/2010 0:00:00]
  10. [Name: Pears; Color: Green; Label: x; CreatedAt: 7/05/2008 0:00:00]
  11.  
  12. List sorted on Color
  13. [Name: Pears; Color: Green; Label: x; CreatedAt: 7/05/2008 0:00:00]
  14. [Name: Apples; Color: Red; Label: y; CreatedAt: 1/07/2010 0:00:00]
  15. [Name: Bananas; Color: Yellow; Label: z; CreatedAt: 9/12/2010 0:00:00]
  16.  
  17. List sorted on Label
  18. [Name: Pears; Color: Green; Label: x; CreatedAt: 7/05/2008 0:00:00]
  19. [Name: Apples; Color: Red; Label: y; CreatedAt: 1/07/2010 0:00:00]
  20. [Name: Bananas; Color: Yellow; Label: z; CreatedAt: 9/12/2010 0:00:00]
  21.  
  22. List sorted on CreationDate
  23. [Name: Pears; Color: Green; Label: x; CreatedAt: 7/05/2008 0:00:00]
  24. [Name: Apples; Color: Red; Label: y; CreatedAt: 1/07/2010 0:00:00]
  25. [Name: Bananas; Color: Yellow; Label: z; CreatedAt: 9/12/2010 0:00:00]
Rating 4.00 out of 5
[?]