user1584282
user1584282

Reputation: 3

sort ArrayCollection by date then time - Flex

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();
}

Upvotes: 0

Views: 3716

Answers (2)

dannrob
dannrob

Reputation: 1069

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;

Upvotes: 0

Ivan Dyachenko
Ivan Dyachenko

Reputation: 1348

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();
}

Upvotes: 2

Related Questions