orderBy not working as expected: Angularjs

Bhumi Singhal picture Bhumi Singhal · May 9, 2013 · Viewed 27.7k times · Source

My array is : BS. Its structure is :

Array[317]
0: Object
   $$hashKey: "022"
   name: "Los Angeles Comm."
.
.
.
..

BS is an array. Each value is a JSon object with filed of name.

I want to sort all the values of BS according to their name. I am trying :

<option ng-repeat="item in BS | orderBy:item.name"  value="{{item.name}}">{{item.name}}</option>

I have also tried : orderBy:name and orderBy:item[name]. Nothing works. Why is this not working and whats the correct code?

Answer

Ajay Beniwal picture Ajay Beniwal · May 9, 2013

Have a look at below html

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
</head>
<body>
    <div ng-controller="item">
        <ul>
            <li ng-repeat="item in items|orderBy:'name'">
                {{item.name}}
            </li>
        </ul>
    </div>
    <script>
        var AppModule = angular.module('app', []);
        function item($scope) {
            $scope.items = [{ name: 'tur' }, { name: 'abc' }, { name: 'xyx' }];

        }
    </script>
</body>
</html>