开发者

Flex:Problems in DataGrid default sorting

开发者 https://www.devze.com 2022-12-14 00:50 出处:网络
I have a datagrid with data like this: Column1   Column2     1               10

I have a datagrid with data like this:

Column1     Column2

    1                 10

    2                 11

    3                 10

    4                 10

When clicking Column2 and using Default Sort,the datagrid turns into this:

Column1     Column2

    3                 10

    1                 10

    4                 10

    2                 11

Why's that?I suppose it should be:

Column1     Column2

    1                 10

    3                 10

  &开发者_如何学Cnbsp; 4                 10

    2                 11

What's wrong with default sorting?Could anyone tell me how to fix it?

Thanks!


This is an interesting question. I looked into it and found that in the end Array.sortOn() function is getting called. In the description of that function there's a line saying:

  • The array is modified to reflect the sort order; multiple elements that have identical sort fields are placed consecutively in the sorted array in no particular order.

Having no way to look into the code, I would make an educated guess that the choice of not keeping the previous order of the items is dictated by the efficiency cost of it, and no real reason to keep the order in most cases. And that's the behavior the default 1 column sorting reflects.

In your case, a quick and dirty (as it's a saturday :) ) example of how to get the result you want. Your datagrid:

<mx:DataGrid id="DG" >
    <mx:columns>
        <mx:DataGridColumn  dataField="0"  />
        <mx:DataGridColumn  dataField="1" sortCompareFunction="testsortCompareFunction" />
    </mx:columns>
</mx:DataGrid>

The sort function:

private function testsortCompareFunction(object1:Object,object2:Object):int
        {
            if (object1[1]>object2[1]) return 1;
            if (object1[1]<object2[1]) return -1;
            if (object1[1]==object2[1]) 
            {
                if (object1[0]>object2[0]) return 1;
                if (object1[0]<object2[0]) return -1;
            }
            return 0;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号