You should add a listener to the DataGrid and then sort the dataprovider. Here is a quick example, also sorting both ways Asc<>Desc (keeping track if a column is sorted one way and sorting it the other way next time)

myDataGrid.addColumn("Product");
myDataGrid.addColumn("Quality");
myDataGrid.addColumn("Quantity");

var myDP:Array = [];
myDP.push({Product:"abc", Quality:145, Quantity:7});
myDP.push({Product:"bcd", Quality:79, Quantity:0});
myDP.push({Product:"def", Quality:300, Quantity:24});
myDP.push({Product:"efg", Quality:138, Quantity:10});

myDataGrid.dataProvider = myDP;

var productSorted:Boolean = true;
var qualitySorted:Boolean = true;
var quantitySorted:Boolean = true;

// Create listener object for DataGrid.
var listener_obj:Object = new Object();
listener_obj.headerRelease = function(evt_obj:Object)
{
switch (evt_obj.target.columns[evt_obj.columnIndex].columnName)
{
   case "Product" :
   if(productSorted) myDP.sortOn("Product", Array.CASEINSENSITIVE);
   else myDP.sortOn("Product", Array.DESCENDING | Array.CASEINSENSITIVE);
   productSorted = !productSorted;
   break;
   case "Quality" :
   if(qualitySorted) myDP.sortOn("Quality", Array.NUMERIC);
   else myDP.sortOn("Quality", Array.DESCENDING | Array.NUMERIC);
   qualitySorted = !qualitySorted;
   break;
   case "Quantity" :
   if(quantitySorted) myDP.sortOn("Quantity", Array.NUMERIC);
   else myDP.sortOn("Quantity", Array.DESCENDING | Array.NUMERIC);
   quantitySorted = !quantitySorted;
   break;
}
};

// Add listener to DataGrid.
myDataGrid.addEventListener("headerRelease", listener_obj);



Merrill, Jason wrote:
Need some help on sorting a datagrid by a particular collumn. Right now,
it's sorting alphabetically by data in the second collumn instead of the
first.  I tried sorting the dataprovider first with the array sort
methods, but they didn't work and the help docs indicate those array
sorting methods don't work on associative arrays.
I have tried:

        lobAdmins_dg.dataProvider.sortOn("lob", Array.DESCENDING);

and also with the DataSetIterator:

        lobAdmins_dg.dataProvider.addSort("lobSort", "lob",
DataSetIterator.Descending);
and:

        lobAdmins_dg.dataProvider.addSort("lobSort", ["lob"],
DataSetIterator.Descending);
and then setting the dataprovider to the datagrid. (lob is the name of
the datagrid collumn and also the property/field in the dataprovider I
want to sort on alphabetically. Also searched help docs, Adobe and Google but no luck. Any ideas?

Jason Merrill
Bank of America Global Technology & Operations Learning & Leadership Development eTools & Multimedia Team


_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to