sort ArrayCollection by date then time - Flex

江枫思渺然 提交于 2020-02-07 23:44:53

问题


I have an ArrayCollection that I'd like to sort by date and then time (in case there's two items with the same date). I've got it sorting by date fine (YYYY/MM/DD) but I can't figure out the time bit, time is in 24 hour format so the code would be basically the same as for the date.

This is the code I used for the date sorting, it works fine.

import mx.collections.SortField;
import mx.collections.Sort;

private function sort():void  
{
var dataSortField:SortField = new SortField();
dataSortField.name = "date";

var arrayDataSort:Sort = new Sort();
arrayDataSort.fields = [dataSortField];

reminderXMLArray.sort = arrayDataSort;
reminderXMLArray.refresh();
}

回答1:


You can use this code to sort by date and time:

private function sort():void
{
    var dataSortField:SortField = new SortField();
    dataSortField.name = "date";
    dataSortField.compareFunction = function (a:Object, b:Object) : int {
        var na:Number = a.date.getTime();
        var nb:Number = b.date.getTime();

        if (na < nb)
            return -1;

        if (na > nb)
            return 1;

        return 0;
    };

    var arrayDataSort:Sort = new Sort();
    arrayDataSort.fields = [dataSortField];

    reminderXMLArray.sort = arrayDataSort;
    reminderXMLArray.refresh();
}



回答2:


As there are two separate fields that you want to sort on you can just use the Sort object's fields Array to add two sort fields:

var sort:Sort = new Sort();
var fields:Array = [ new SortField("date"), new SortField("time") ];
sort.fields = sort;


来源:https://stackoverflow.com/questions/12398520/sort-arraycollection-by-date-then-time-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!