Angular: Sorting by date1

http://angularjs.org/

by manoj

HTML

<div ng-app="app">
    <script type="text/javascript" src="http://code.angularjs.org/snapshot/angular.js"></script>
    <div ng:controller="Main">
        <div ng:repeat="item in itemList | orderBy: 'value.date'">
            {{item.key}}: {{item.value.date | date}}
            <button ng-click="remove(item)">Remove</button>
        </div>
        <button ng-click="add()">Add</button>
    </div>
</div>

JavaScript

angular.module('app', [])

.controller('Main', function Main($scope) {
    $scope.nextKey = 4;
    $scope.items = {
        0: {date: new Date('12/23/2013')},
        1: {date: new Date('12/23/2011')},
        2: {date: new Date('12/23/2010')},
        3: {date: new Date('12/23/2015')}
    };
    
    $scope.remove = function(item) {
        delete $scope.items[item.key];
    };
    
    $scope.add = function() {
        $scope.items[$scope.nextKey] = { date: new Date() };
        $scope.nextKey += 1;
    };
    
    $scope.itemList = [];
    $scope.$watchCollection('items', function(items) {
        $scope.itemList.length = 0;
        angular.forEach(items, function(value, key) {
            $scope.itemList.push({ key: key, value: value});
        });
    });
});