2

the default sort of ng-grid is not alphanumeric. is it possible to apply a custom sort function in order to obtain a correct sort, as :

'11' '2' 'eé' 'ed' => '2' '11' 'ed' 'eé'

2 Answers 2

3

You could define your own sorting function.

Here's an example with your data. (Fixed it using Devin Torres' Natural Sorting algorithm)

var myAwesomeSortFn = function(a,b){

  var NUMBER_GROUPS = /(-?\d*\.?\d+)/g;

  var myAwesomeSortFn = function(a, b) {

    var aa = String(a).split(NUMBER_GROUPS),
    bb = String(b).split(NUMBER_GROUPS),
    min = Math.min(aa.length, bb.length);

    for (var i = 0; i < min; i++) {
      var x = parseFloat(aa[i]) || aa[i].toLowerCase(),
      y = parseFloat(bb[i]) || bb[i].toLowerCase();
      if (x < y) return -1;
      else if (x > y) return 1;
    }

  return 0;
};

$scope.myData = [{name: "Moroni", age: 11},
                 {name: "Tiancum", age: 2},
                 {name: "Jacob", age: 'eé'},
                 {name: "Nephi", age: 'ed'}];  
$scope.gridOptions = { 
    data: 'myData',
    columnDefs: [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age', sortFn: myAwesomeSortFn}]
};
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like in my version of UI Grid, the property is called sortingAlgorithm instead of sortFn.
0

Here is what I have so far:

        $scope.links = {
            sortFun: function(a, b) { console.log('a:' + a + ', b: ' + b); }
        };

...


$scope.data.grid.columnDefs.push(
  {'field': key, 'sortingAlgorithm': $scope.links.sortFun }
);   

...

                $scope.data.grid.columnDefs.push({'field': key, 'sortingAlgorithm': $scope.links.sortFun });  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.