Just off the top of my head, probably have 1 and -1 mixed up :)
function createCompareFunction(fieldName : String) : Function
{
return function(left : Object, right : Object) : Number
{
if (left[fieldName] < right[fieldName])
return -1;
if (left[fieldName] > right[fieldName])
return 1;
return 0;
}
}
Then you can just call:
createCompareFunction("field1");
createCompareFunction("field2");
createCompareFunction("fieldN");
-Josh
2009/3/15 Dharmendra Chauhan <[email protected]>
> Hi All,
> My flex2 *DataGrid* has got at least *50 columns* of type Date.I
> also have list which opens when use select a context menu item ,in this
> list user can select any no of columns on which he wants multiple sorting
> to be happen.
>
> Lets say user has selected 5 Columns for multi sorting,I create 5 sortField
> with compareFunction in loop as follows
>
> var sortFieldArray:Array = new Array();
> for(var *dataField *in *selectedColumnArray*){
> var field:SortField = new SortField(dataField );
>
> field.compareFunction = function(itemA:Object,itemB:Object):int{
> var dateA:Date =itemA[*dataField *] ;
> var dateB:Date =itemB[*dataField *] ;
>
> .....
> }
> sortFieldArray.push(field);
> }
> Problem which I have been facing is ,local var dataField is always
> referring to the last value of *selectedColumnArray *hence sorting is
> happening based on last column only.
>
> I dont want to create separate compareFunction for each column , simply
> because at compilation time I dont know about the no of column which user
> can choose to participate in multipleSorting.And I dont want to create 50
> compareFunction with *hardcoded dataField* name
>
>
> Is it possible to create *GENERIC compareFucntion *which can take
> dataField without hard coding ?
>
> Please throw some light on this it?
>
>
> Regards,
> Dharmendra chauhan
>
>
>
>
>
>
>
>
>
>
>
>
--
"Therefore, send not to know For whom the bell tolls. It tolls for thee."
Josh 'G-Funk' McDonald
- [email protected]
- http://twitter.com/sophistifunk
- http://flex.joshmcdonald.info/