How do you update ng-grid gridOptions.sortInfo?

Problem

I am trying to programmatically update the gridOptions.sortInfo on the Angular grid ng-grid, but I can't get it working.

I have both "name" and "age" columns. I am initially setting the sort on the "name" column but would like load some new data and then update it to sort on "age". (Programmatically, not just by clicking the column header).

I can set the $scope.gridOptions.sortInfo to a new value, but the grid does not reflect this. What is the correct way to update the gridOptions.sortInfo ?

Please see plunker:

http://plnkr.co/edit/JBYnrwLAIwFSKS6uSAND?p=preview

EDIT: Please note I would like to be able to update the sort direction i.e. ascending/descending as well as the actual column to sort on.

Many thanks

Problem courtesy of: EdL

Solution

Yes, according to source code (line number 128 here link) you should be able to just do like this:

$scope.updateSortInfo = function() {
  $scope.gridOptions.sortBy('name');
}

The plunker shows that it works. I forked it here.

Here's another version with sortColumn passed in from a text box: plunker.

Solution courtesy of: marko

Discussion

Look at this: http://ui-grid.info/docs/#/api/ui.grid.class:GridOptions.columnDef

Search for this text:

sort An object of sort information, attributes are:

I needed to sort by the id column at the beginning in desc order and that did the trick for me:

var columnsDef = [
            {name:'id',width:50,sort: {
                direction: 'desc',
                priority: 0
            }},
            {name:'title',width:350},
            {name:'address'},
            {name:'date',width:150},
            {name:'description',minWidth:450}
        ];
$scope.theGrid.columnDefs = columnsDef;
Discussion courtesy of: Natan Rubinstein

Turns out if you call $scope.gridOptions.sortBy() twice on the same column, it will sort in descending order.

Discussion courtesy of: Mitch Penrod

This recipe can be found in it's original form on Stack Over Flow.