开发者

Sorting dates in adg

开发者 https://www.devze.com 2023-02-22 01:53 出处:网络
I have an advancedDataGrid and I would like to sort the adgaccording oneAdvancedDataGridColumn(studyDate) which use strings in format DD/MM/YYYY(I receive like this from the server):

I have an advancedDataGrid and I would like to sort the adg according one AdvancedDataGridColumn(studyDate) which use strings in format DD/MM/YYYY(I receive like this from the server):

<mx:AdvancedDataGrid id="myADG" width="100%" height="100%" color="0x323232" dataProvider="{_currentDatosBusqueda}" verticalScrollPolicy="auto" 
        fontSize="11" fontFamily="Arial" fontStyle="normal" fontWeight="normal" doubleClickEnabled="true"
    itemDoubleClick="dobleClickFilaDataGridBusqueda(event);" useRollOver="true"
                                                   >  

    <mx:AdvancedDataGridColumn headerText="Fecha del estudio" dataField="studyDate"  paddingRight="0" textAlign="right" resizable="false"/>

And this is the function I use to sort:

private function sortData():void {
            var sort:Sort = new Sort();
            var sortField:SortField = new SortField("studyDate", true, true);
            var sortField2:SortField = new SortField("studyTime", true, false);

            sort.fields = [sortField, sortField2];        
            _currentDatosBusqueda.sort = sort;   

            _currentDatosBusqueda.refresh();         
        }

But it only sort attendind the day, I mean:

12/02/2011

23/03/2011

25/02/2011

It sorts like this:

25/02/2011

23/03/2011

12/02/2011

I try using a sortCompareFunction but it does not work(probably I'm doing wrong) so can someone give me any idea???

Thanks in advance

EDIT:

Finally I find my problem, I just tried to sort dates with the format DD/MM/YYYY so I need to convert to MM/dd/yyyy f开发者_开发知识库ormat before sorting. This is the function I make:

private function date_sortCompareFunc(itemA:Object, itemB:Object, fields:Array = null):int {
            var year:int = int(itemA.studyDate.substr(6,4));
            var month:int = int(itemA.studyDate.substr(3,2))-1;
            var day:int = int(itemA.studyDate.substr(0,2));

            var dateA : Date = new Date(year, month, day);

            year = int(itemB.studyDate.substr(6,4));
            month = int(itemB.studyDate.substr(3,2))-1;
            day = int(itemB.studyDate.substr(0,2));

            var dateB : Date = new Date(year, month, day);

        //  return ObjectUtil.dateCompare(dateA, dateB);

            return ( dateA.valueOf() > dateB.valueOf() ) ? 1 : ( dateA.valueOf() < dateB.valueOf() ) ? -1 : 0;
        }

But i find another problem, I need to sort the columns of the adg the first time it displays so I used the following function but It doesn't sort from the recent day to old one, I do not know what I can I do cause I set sort.descending= true, any ideas?

private function sortData():void {
            var sort:Sort = new Sort();
            var sortField:SortField = new SortField("studyDate", true, true);
            sortField.descending = true;
            var sortField2:SortField = new SortField("studyTime", true, false);

            sort.fields = [sortField, sortField2];        
            sort.compareFunction = date_sortCompareFunc;            
            _currentDatosBusqueda.sort = sort;   
            _currentDatosBusqueda.refresh();  

        }


You can use the Object util to do something like:

private function dateSortCompare(itemA:YourObject, itemB:YourObject):int 
{
  var dateA:Date = new Date(Date.parse(itemA.date));
  var dateB:Date = new Date(Date.parse(itemB.date));
  return ObjectUtil.dateCompare(dateA, dateB);
}

and then in your column:

 <mx:AdvancedDataGridColumn headerText="Fecha del estudio" dataField="studyDate"  paddingRight="0" textAlign="right" resizable="false" sortCompareFunction="dateSortCompare"/>

or do the Date.time() and sort the numbers wich ever way you want..

This is pseudo code, I have no idea if it compiles, but it should give you an idea of how to go about it. Good luck :)

0

精彩评论

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

关注公众号