MrProgram
MrProgram

Reputation: 5242

custom sortfunction that are case insensitive

I have a ng-repeat where I the orderBy-function. I've noticed that it's not working as I would like it to do when it comes to case sensitivity

In my example you see that "BBBBB" comes before "aaaaa", which is wrong in my case. "aaaaa" should be ordered before "BBBBB".

I'm not sure how to do this..Can anyone help me?

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

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example105-production</title>
<link href="style.css" rel="stylesheet" type="text/css">


<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<script src="script.js"></script>



</head>
<body ng-app="orderByExample">
<div ng-controller="ExampleController">
<table class="friend">

<tr>
  <th>
     <button ng-click="order('group')">Name</button>
     <span class="sortorder" ng-show="predicate === 'group'" ng-class="{reverse:reverse}"></span>
 </th>
<tr ng-repeat="friend in friends | orderBy:predicate:reverse">
  <td>{{friend.group}}</td>
</tr>
</table>
</div>
</body>
</html>

js:

(function (angular) {
    'use strict';
    angular.module('orderByExample', [])
        .controller('ExampleController', [
            '$scope',
            function ($scope) {
                $scope.friends = [
                   { group: ['aaaaa'] },
                   { group: ['BBBBB'] },
                   { group: ['AAAA'] },
                ];
                $scope.predicate = 'group';
                $scope.reverse = false;
                $scope.order = function (predicate) {
                    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
                    $scope.predicate = 'group';
                };
            }
        ]);
})(window.angular);

Upvotes: 0

Views: 94

Answers (3)

jonathanfann
jonathanfann

Reputation: 46

The problem isn't actually the case. It's due to the fact that your data is not comprised of strings. Change the data structure to be like so:

$scope.friends =
    [{group:'aaaaa'},
    {group:'BBBBB'},
    {group:'AAAA'},
];

Upvotes: 1

Nils
Nils

Reputation: 806

Change your predicate as

   $scope.predicate = function(item) {
     return item.group.join('').toUpperCase();
  };

Upvotes: 0

George Houpis
George Houpis

Reputation: 1729

Change your predicate to a function:

$scope.predicate = function(d) { return d.group[0].toLowerCase(); };

Upvotes: 1

Related Questions