This project is read-only.

Sorting null causes exception

Jul 9, 2009 at 4:35 PM
We uncovered a problem with the BoundList (used by DataGrid as a DataSource) when trying to sort models who's value is null. The issue is easily fixed but comes into play in the ApplySort method. It is not properly handling null and should return 0 when comparing two items that are null. Specifically this code:

 

if (valx != null && valy != null)
{
    int result = valx.CompareTo(valy);
    if (result != 0)
        return result;
}
else if (valx != null)
    return 1;
else
    return -1;
The final else should be changed to:
else (valy != null)
    return -1;
this will cause the null null comparison to fall through to the surrounding return 0 which is how it should sort.
Hope this helps ...
Sean

 

if (valx != null && valy != null)
                        {
                            int result = valx.CompareTo(valy);
                            if (result != 0)
                                return result;
                        }
                        else if (valx != null)
                            return 1;
                        else
                            return -1;should
Jul 14, 2009 at 8:03 AM

Where is the code that you are reffering to?