Sorting by patients' name or birth Is there a way to write this simpler?

if(criteria.SortBy =="name")
{
    if (criteria.SortDirection == "ascending")
    {
        physicians.Sort((x, y) =&gt; string.Compare(x.Name, y.Name));
    }
    else
    {
        physicians.Sort((x, y) =&gt; string.Compare(y.Name,x.Name ));

    }
}

if (criteria.SortBy == "birth")
{
    if (criteria.SortDirection == "ascending")
    {
        physicians.Sort(new Comparison&lt;Physician&gt;((x, y) =&gt; DateTime.Compare(x.DateOfBirth ?? DateTime.MinValue,y.DateOfBirth ?? DateTime.MinValue)));
    }
    else
    {
        physicians.Sort(new Comparison&lt;Physician&gt;((x, y) =&gt; DateTime.Compare(y.DateOfBirth ?? DateTime.MinValue, x.DateOfBirth ?? DateTime.MinValue)));

    }
}
You can use the ComparerExtensions library to help you create comparers more easily.

With it, your code could look something like:

IComparer&lt;Physician&gt; comparer = null;

if (criteria.SortBy == "name")
{
    comparer = KeyComparer&lt;Physician&gt;.OrderBy(x =&gt; x.Name);
}
else if (criteria.SortBy == "birth")
{
    comparer = KeyComparer&lt;Physician&gt;.OrderBy(x =&gt; x.DateOfBirth);
}

if (comparer != null)
{
    if (criteria.SortDirection != "ascending")
    {
        comparer = comparer.Reversed();
    }

    physicians.Sort(comparer);
}