0

I would like to sort my gridcolumn.Here's my sortCompareFunction code:

protected function sortCompareFunction(obj1:Object, obj2:Object, gc:GridColumn):int
        {
            collator.ignoreCase=true;
            return collator.compare(obj1[gc.dataField], obj2[gc.dataField]);
        }

and the datagrid:

<s:DataGrid id="dataGrid" width="100%" height="100%" borderColor="#CCCCCC" borderVisible="true"
            chromeColor="#CCCCCC" color="#000000" contentBackgroundColor="#8C90BB"
            selectionColor="#D0E4E9" symbolColor="#FFFFFF" sortableColumns="true">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn width="150" dataField="title" headerText="Progetto"
                          sortCompareFunction="sortCompareFunction"></s:GridColumn>
            <s:GridColumn dataField="author" headerText="Stato" width="50"></s:GridColumn>
            <s:GridColumn dataField="newsdate" headerText="Scadenza"></s:GridColumn>
        </s:ArrayList>
    </s:columns>
    <s:AsyncListView list="{FlexGlobals.topLevelApplication.PMProjs.lastResult}"/>
</s:DataGrid>

It doesn't work. I don't see the arrow in the column header and anything is sorted. Thanks in advance for any help!

Ok, solved by substituting the AsyncListView with an Arraycollection:

<s:DataGrid id="dataGrid" width="100%" height="100%" borderColor="#CCCCCC" borderVisible="true"
            chromeColor="#CCCCCC" color="#000000" contentBackgroundColor="#8C90BB"
            selectionColor="#D0E4E9" symbolColor="#FFFFFF">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn width="150" dataField="title" headerText="Progetto" 
                          showDataTips="title"></s:GridColumn>
            <s:GridColumn dataField="author" headerText="Stato" width="50"></s:GridColumn>
            <s:GridColumn dataField="newsdate" headerText="Scadenza"></s:GridColumn>
        </s:ArrayList>
    </s:columns>
    <s:ArrayCollection list="{FlexGlobals.topLevelApplication.PMProjs.lastResult}"/>

without sortCompareFunction.

chu
  • 21
  • 5
  • I believe you may have to bind the sort function instead of just using its name (i.e. add the accolades `{}`). Other than that the error can only be this `collator` class. – RIAstar Oct 12 '12 at 08:59
  • Thank you for reply. If you mean sortCompareFunction="{sortCompareFunction}" it doesn't work. I tried also to use return ObjectUtil.stringCompare(obj1, obj2), with obj1 and obj2 as String but it doesn'y have effect. – chu Oct 12 '12 at 10:33
  • Perhaps it's related to the `AsyncListView` then. Try replacing it with a aimple ArrayCollection – RIAstar Oct 12 '12 at 11:25
  • Perfect!Now it sorts the column!i don't set my function as sortCompareFunction because it works in a wrong way(i don't understand how it works, i use it in a bad way for sure!), and i read in documentation that gridColumn use to sort by default by datafield. So the asyncListView avoided sorting. Could you explain me why please?Thank you very very much for your help! – chu Oct 12 '12 at 13:56
  • Instead of editing the question to add "solved" in the title; please post the solution as a formal answer to the question. – JeffryHouser Oct 12 '12 at 14:40

2 Answers2

0

Yes. As suggested by RIAstar, changing AsyncListView to ArrayCollection will do the trick. Note that asynclist is data structure that is generated by Flex.

Also note that the Arraylist defined inside the columns tag of the DataGrid does NOT have to be changed to Arraycollection. Interesting.

0
protected function sortTtl_changeHandler(event:IndexChangeEvent):void
        {
            switch(event.newIndex)
            {
                case 0:
                {
                    var sort:Sort = new Sort();
                    sort.fields = [new SortField("title", true)];
                    dataGrid.sort = sort;
                    dataGrid.refresh();
                    break;
                }
                case 1:
                {
                    var sort:Sort = new Sort();
                    sort.fields = [new SortField("author", true)];
                    dataGrid.sort = sort;
                    dataGrid.refresh();
                    break;
                }
                case 2:
                {
                    var sort:Sort = new Sort();
                    sort.fields = [new SortField("newsdate", true)];
                    dataGrid.sort = sort;
                    dataGrid.refresh();
                    break;
                }

                default:
                {
                    break;
                }
            }

        }
    <s:DropDownList id="sortTtl" visible="false" width="128"
                                change="sortTtl_changeHandler(event)" dataProvider="{sortData}"
                                labelField="label"/>    
Ravi
  • 1
  • 2