I think Mike Chambers' method of sorting (http://weblogs.macromedia.com/mesh/archives/2005/04/sorting_date_fi.cfm - similar to below) works pretty good, but it just seems wrong that you have to wait for the DataGrid to sort, react to the headerRelease function, which occurs after the sort and then resort again with your own algorithm. The sortCompareFunction would have been great if it would just return row data, similar to the labelFunction.

Also, the LiveDocs say "headerRelease: Broadcast when a column header is pressed and released, indicating that a sort operation is about to occur." But I don't think this method would work if that were true. Good thing it happens after the sort!

Sean



On Jun 15, 2005, at 3:48 AM, Dirk Eismann wrote:

You can sort Date objects by sorting on their numerical getTime() value, i.e.

<mx:Script>
  private function sortColumn(evt:Object):Void {
    if (evt.target.sortDirection == "DESC") {
      var arrayFlags:Number = Array.DESCENDING;
    } else {
      var arrayFlags:Number = Array.ASCENDING;
    }
    evt.target.sortItems(sortByDate, arrayFlags);
  }

  public function sortByDate(itemA:Object, itemB:Object, flags:Number):Number {
    var a:Date = itemA.date;
    var b:Date = itemB.date;
    if (flags == Array.ASCENDING) {
      if (a.getTime() < b.getTime()) return -1;
      if (a.getTime() == b.getTime()) return 0;
      if (a.getTime() > b.getTime()) return 1;
    } else {
      if (a.getTime() < b.getTime()) return 1;
      if (a.getTime() == b.getTime()) return 0;
      if (a.getTime() > b.getTime()) return -1;
    }
  }
</mx:Script>

<mx:DataGrid
  id="dateGrid"
  dataProvider="{data}"
  headerRelease="sortColumn(event)"
  >
  <mx:columns>
    <mx:Array>
      <mx:DataGridColumn sortable="true" columnName="date" headerText="Date" />
    </mx:Array>
  </mx:columns>
</mx:DataGrid>


Dirk.


> -----Original Message-----
> From: flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com]On
> Behalf Of Sean McKibben
> Sent: Tuesday, June 14, 2005 9:35 PM
> To: flexcoders@yahoogroups.com
> Subject: [flexcoders] Design flaw in DataGrid sorting
>
>
> I have to call the data grid's sorting mechanism flawed.
>
> Let's examine a case where you want to sort a dataGrid by a date 
> column. In all likelihood, you're going to have a labelFunction to 
> format your date objects into a string. Depending on your locale, 
> that string is probably not what you want to sort on (i.e. 02/04/05 
> comes before 02/03/06 on the calendar, but not as a string),
> so you'd 
> like to write a sortCompare function for that column.
>
> Unfortunately, the sortCompare function only receives the string 
> value from the labelFunction, so you'd either have to do some very 
> slow string parsing, or sort based on the results from the 
> labelFunction alone. You don't get a chance to compare any more 
> information about a row than the string returned by a labelFunction, 
> and the labelFunction can only return a string.
>
> Not a good way to do it, Macromedia!
>
> So, what is the best workaround? Let it sort once, then resort using 
> the headerRelease event and operating on the DataProvider? (assuming 
> headerRelease fires after sorting is done - contrary to the 
> documentation, but in line with Ailstair McLeod's tests)
>
>
> Sean
>
>
>

> Yahoo! Groups Links
>
>
>

>
>
>
>


Yahoo! Groups Links




Yahoo! Groups Links

Reply via email to