1

I've done a basic table sorting application using angular, sorting is doing but the problem is that i'm not getting the values sorted in ASCII value

can anyone please tell me how to do it

[DEMO]

html

    <div ng-app ng-controller="ArrayController">
        <table>
            <th ng-repeat="header in headers">
                <a ng-click="sort_by(header)">{{ headers[$index] }}</a>
            </th>
            <tr ng-repeat="arr in records |orderBy:sortingOrder:reverse">
                <td ng-repeat="val in arr" ng-bind-html-unsafe="arr[headers[$index]]"></td>
            </tr>
        </table>
    </div>

script

    function ArrayController($scope) {
    $scope.headers = ['col1', 'col2'];
    $scope.records = [{col1: 'a1', col2: 'd1'}, {col1: 'c2', col2: 'A2'}, {col1: 'b3', col2: 'c3'}, {col1: 'd4', col2: 'a1'}];

    $scope.reverse = true;
     $scope.sort_by = function(newSortingOrder)
        {
            if ($scope.sortingOrder === newSortingOrder)
            {
              $scope.reverse = !$scope.reverse;
            }
            $scope.sortingOrder = newSortingOrder;

          };
}
Alex Man
  • 4,746
  • 17
  • 93
  • 178

1 Answers1

1

You need to write a custom orderBy function, check this post for more details.

Community
  • 1
  • 1
Ron
  • 6,037
  • 4
  • 33
  • 52
  • here you go, [jsfiddle](http://jsfiddle.net/uRPSL/19/), the main point is charCodeAt. – Ron Mar 12 '14 at 12:23
  • yes...this is my custom sort method `$scope.myValueFunction = function(values) { $scope.cards.sort(function(str1, str2) { console.log(str1.name); console.log(str2.name); return ( ( ('aaa').compareTo('sds') ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) ); }); }`...........but i'm getting TypeError: Object aaa has no method 'compareTo' – Alex Man Mar 12 '14 at 12:28
  • Seems in javascript, string doesn't have compareTo method. – Ron Mar 12 '14 at 12:40
  • sorry, I just realised I didn't save the jsfiddle, actually I already did, let me try to rewrite. – Ron Mar 12 '14 at 12:51