Can Sorting be performed on a Flex arrayCollection based on date/time values instead of normal text string / alpha?

StackOverflow https://stackoverflow.com/questions/19387423

質問

I have a flex Array Collection created from a live XML data source and am trying to use my date/time string in the array to SORT the array prior to having the UI display the info / listing... currently the array is created and displays fine but the sorting by date / time is NOT working properly...

The routine works if I change the sort field (dataSortField.name) to 'name' (just alphanumeric text string based on filenames generated by my xml source), but if I use 'datemodified' as the sort field ( i.e. 7/24/2013 12:53:02 PM ) it doesn't sort it by date, just tries to sort alphabetically so the date order is not proper at all and for example it shows 1/10/2013 10:41:57 PM then instead of 2/1/2013 11:00:00 PM next it shows 10/10/2013 5:37:18 PM. So its using the date/time as a regular text string

// SORTING THE ARRAY BY DATE DESCENDING...
  var dataSortField:SortField = new SortField();
  dataSortField.name = "datemodified";
  dataSortField.descending = false;

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

  arr.sort = arrayDataSort;
  arr.refresh();

Now if I CHANGE the dataSortField.name to "name" (which are alphanumeric filenames) it sorts a-z just fine... so How do I get it to sort by DATE where my array data looks like 7/24/2013 12:00:00 PM

Now the TIME part of the date isnt necessary for my sorting needs at all, so Im just looking to sort by date and beyond that the time doesnt matter for my needs but is hard coded in my xml data source.

I tried specifying

dataSortField.numeric = true;

but that didnt work either and while I can use it to specify string or numeric theres not a DATE option as I was expecting.

so my question, to clarify, is how do I make the SORT function acknowledge that I want to sort based on a series of date / time stamps in my array? Im using apache flex 4.9.1 / fb 4.6 premium).

役に立ちましたか?

解決

I use this as a date compare function:

public static function genericSortCompareFunction_Date(obj1:Object, obj2:Object):int{
    //  * -1 if obj1 should appear before obj2 in ascending order.
    //  * 0 if obj1 = obj2.
    //  * 1 if obj1 should appear after obj2 in ascending order.

    // if you have an XML Datasource; you'll have to do something here to get the 
    // date objects out of your XML and into value1 and value2
    var value1:Date = obj1.dateField;
    var value2:Date = obj2.dateField;

    if(value1 == value2){
        return 0;
    }
    if(value1 < value2){
        return -1;
    }
    return 1;
}   

To apply this to your code; you would do something like this:

  var arrayDataSort:Sort = new Sort();
  arrayDataSort.compareFunction = genericSortCompareFunction_Date;

  arr.sort = arrayDataSort;
  arr.refresh();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top