List.Sort() fails when the list has null values
<p>I have a user control in that a grid gets data using ObjectDataSource. All the columns in the grid are sortable. If the user clicks on a particular column name, the list is sorted based on that column (List.Sort(sortColumn)). </p>

<p>I am facing an issue when one of the column has blank / null value in their field. The comparison line strA.CompareTo(strB) fails with "Object reference not set to an instance of an object" when strA/ strB is null or both are null.</p>

<p>How ever, I have included !string.IsNullOrEmpty() for strA and strB to avoid the null refernce exception. Still it doesn't sort the grid.</p>

<p>The code snippet is given below.</p>

<pre><code>int IComparer&lt;MyWorklistItem&gt;.Compare(MyWorkItem x, MyWorkItem y)
{
        int sortValue = 1;
        if (this.strSortField.Length == 0)
        {
            return 0;
        }
        MyWorkItem item1 = this.blnSortDesc ? y : x;
        MyWorkItem item2 = this.blnSortDesc ? x : y;

        PropertyInfo property1 = item1.GetType().GetProperty(this.strSortField);
        PropertyInfo property2 = item2.GetType().GetProperty(this.strSortField);

        string strA = (string)property1.GetValue(item1, null);
        string strB = (string)property2.GetValue(item2, null);

        if (!string.IsNullOrEmpty(strA) &amp;&amp; !string.IsNullOrEmpty(strB))
        {               
            sortValue = strA.CompareTo(strB);
        }           
        return sortValue;
    }
</code></pre>

<p>How do I sort, when one of the values or both are null. </p>

<p><strong>Note: I am using VS 2005, so no possibility for LINQ though.</strong> </p>

<p>Please suggest.</p>

<p>Thanks,
Sriram</p>
