Custom DataGrid Sort To Force Nulls To The Bottom

A requirement came up recently requiring that my DataGrid column sort alpha-numerically, but with all the nulls pushed to the bottom. Providing a custom sort without considering the column’s sortDescending attribute worked with descending, but not ascending sorts. The values would be at the bottom for descending with all the nulls at the top. To keep the values sorted and all the nulls at the bottom, regardless of sort direction, you must consider this attribute and reverse the expected return int. It seems as if the framework is taking this into account and reversing it for you during ascending sorts so you must preempt their efforts.

public function sortColumn(a:Object, b:Object, c:GridColumn):int
{
    var ret = 0;

    if ((a.param && a.param.length > 0) && (b.param && b.param.length > 0))
    {
        ret = (a.param < b.param) ? -1 : 1;
    }
    else if ((!a.param || a.param.length == 0) && (b.param && b.param.length > 0))
    {
        ret = (c.sortDescending) ? -1 : 1;
    }
    else if ((a.param && a.param.length > 0) && (!b.param || b.param.length == 0))
    {
        ret = (c.sortDescending) ? 1 : -1;
    }

    return ret;
}

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s