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;

          };
}

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

here you go, jsfiddle, the main point is charCodeAt.
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'
Seems in javascript, string doesn't have compareTo method.
sorry, I just realised I didn't save the jsfiddle, actually I already did, let me try to rewrite.

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.