Sort on underlying data in ng-grid

Viewed 13678

I wish to display a formatted value in a ng-grid cell, but sort on a related numeric value not displayed.

var myData1 = [{
 name: "Moroni",
  age: 50,
  ageWord: "Fifty"
}

From the above example I would display the ageWord column, but wish to sort on the age column.

The docs for sorting an ng-grid directive indicate I can provide a custom function to sort underlying data:

sortFn

Sets the sort function for the column. Useful when you have data that is formatted in an unusual way or if you want to sort on an underlying data type. Check the MDN sort docs for examples

the custom sort function receives the display values when the column sort is clicked, but I wish to sort on the age. If I had the row available in the sort function I could sort on the sibling cell, something like this:

sortFn: function(a, b) {
    var ageA = a.getProperty('age');
    var ageB = b.getProperty('age');
    return ageA-ageB;
  }

Any suggestions how I can sort by 'age' when the 'ageWord' column is sorted? Example available on plnkr.

2 Answers
Related