actionscript 3 - DataGrid - sort a column based on its own values, and secondarily based on another columns values? -
i have advanceddatagrid 2 columns: code (strings), , value (numbers). use same sort function each column. want sort both columns based on value column (numeric data), there no number available, want sorting done alphabetically code column.
i have simplified problem facing example represent trying do:
the picture shows 2 columns, sorting of both columns based on value column. value nan, want code column values sorted alphabetically. one, two, three, 4 remain same, badc abcd.
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minwidth="955" minheight="600" initialize="initializehandler(event)"> <mx:script> <![cdata[ import mx.collections.arraycollection; import mx.events.flexevent; import mx.utils.objectutil; [bindable] private var ac:arraycollection; protected function initializehandler(event:flexevent):void { ac = new arraycollection(); var one:namevalueobject = new namevalueobject("one", 1); var two:namevalueobject = new namevalueobject("two", 2); var three:namevalueobject = new namevalueobject("three", 3); var four:namevalueobject = new namevalueobject("four", 4); var a:namevalueobject = new namevalueobject("a", nan); var b:namevalueobject = new namevalueobject("b", nan); var c:namevalueobject = new namevalueobject("c", nan); var d:namevalueobject = new namevalueobject("d", nan); ac.additem(one); ac.additem(two); ac.additem(three); ac.additem(four); ac.additem(b); ac.additem(a); ac.additem(d); ac.additem(c); } private function numericvaluesort(obj1:object, obj2:object):int { var value1:number = (obj1 namevalueobject).value; var value2:number = (obj2 namevalueobject).value; return objectutil.numericcompare(value1, value2); } private function codelabelfunction(item:object, column:advanceddatagridcolumn):string { return (item namevalueobject).code; } ]]> </mx:script> <mx:advanceddatagrid width="500" height="300" dataprovider="{ac}"> <mx:columns> <mx:advanceddatagridcolumn id="codecolumn" headertext="code" datafield="value" labelfunction="codelabelfunction" sortcomparefunction="numericvaluesort"> </mx:advanceddatagridcolumn> <mx:advanceddatagridcolumn id="numericvaluecolumn" headertext="value" datafield="value" sortcomparefunction="numericvaluesort"> </mx:advanceddatagridcolumn> </mx:columns> </mx:advanceddatagrid> </mx:application>
the navevalueobject class
package { public class namevalueobject { public var code:string; public var value:number; public function namevalueobject(acode:string, anumber:number) { code = acode; value = anumber; } } }
try this:
private function numericvaluesort(obj1:object, obj2:object):int { var o1:namevalueobject = obj1 namevalueobject; var o2:namevalueobject = obj2 namevalueobject; var ret:int = 0; if (isnan(o1.value) || isnan(o2.value)) { if (o1.code > o2.code) { ret = 1; } else if (o1.code < o2.code){ ret = -1; } } else { ret = objectutil.numericcompare(o1.value, o2.value); } return ret; }
your function process number either valued, comparation between code field of 2 objects
Comments
Post a Comment