C# Multiple Property Sort

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Tue, 30 Mar 2010 19:14:25 +0000 Indexed on 2010/03/30 19:23 UTC
Read the original article Hit count: 571

Filed under:
|

As you can see in the snippet below, sorting is easy with Linq.  Simply provide your OrderBy criteria and you’re done.  If you want a secondary sort field, add a ThenBy expression to the chain.  Want a third level sort?  Just add ThenBy along with another sort expression.

  1. var projects = new List<Project>
  2.     {
  3.         new Project {Description = "A", ProjectStatusTypeId = 1},
  4.         new Project {Description = "B", ProjectStatusTypeId = 3},
  5.         new Project {Description = "C", ProjectStatusTypeId = 3},
  6.         new Project {Description = "C", ProjectStatusTypeId = 2},
  7.         new Project {Description = "E", ProjectStatusTypeId = 1},
  8.         new Project {Description = "A", ProjectStatusTypeId = 2},
  9.         new Project {Description = "C", ProjectStatusTypeId = 4},
  10.         new Project {Description = "A", ProjectStatusTypeId = 3}
  11.     };
  12.  
  13. projects = projects
  14.     .OrderBy(x => x.Description)
  15.     .ThenBy(x => x.ProjectStatusTypeId)
  16.     .ToList();
  17.  
  18. foreach (var project in projects)
  19. {
  20.     Console.Out.WriteLine("{0} {1}", project.Description,
  21.         project.ProjectStatusTypeId);
  22. }

Linq offers a great sort solution most of the time, but what if you want or need to do it the old fashioned way?

  1. projects.Sort ((x, y) =>
  2.         Comparer<String>.Default
  3.             .Compare(x.Description, y.Description) != 0 ?
  4.         Comparer<String>.Default
  5.             .Compare(x.Description, y.Description) :
  6.         Comparer<Int32>.Default
  7.             .Compare(x.ProjectStatusTypeId, y.ProjectStatusTypeId));
  8.  
  9. foreach (var project in projects)
  10. {
  11.     Console.Out.WriteLine("{0} {1}", project.Description,
  12.         project.ProjectStatusTypeId);
  13. }

It’s not that bad, right?

Just for fun, let add some additional logic to our sort.  Let’s say we wanted our secondary sort to be based on the name associated with the ProjectStatusTypeId. 

  1. projects.Sort((x, y) =>
  2.        Comparer<String>.Default
  3.             .Compare(x.Description, y.Description) != 0 ?
  4.        Comparer<String>.Default
  5.             .Compare(x.Description, y.Description) :
  6.        Comparer<String>.Default
  7.             .Compare(GetProjectStatusTypeName(x.ProjectStatusTypeId),
  8.                 GetProjectStatusTypeName(y.ProjectStatusTypeId)));
  9.  
  10. foreach (var project in projects)
  11. {
  12.     Console.Out.WriteLine("{0} {1}", project.Description,
  13.         GetProjectStatusTypeName(project.ProjectStatusTypeId));
  14. }

The comparer will now consider the result of the GetProjectStatusTypeName and order the list accordingly.  Of course, you can take this same approach with Linq as well.

© Johnny Coder or respective owner

Related posts about c#

Related posts about LINQ