Search code examples
angularjsangularjs-orderby

AngularJs orderBy filter not working with tracking


I am at a lost on why orderBy is not working, the order of ng-repeat output is the same no matter what property I pass to orderBy e.g orderBy:'-year1comp' or orderBy:'year1comp' has no effect.

$scope.expTest = [{
    "company": "new",
    "schedule": "fulltime",
    "titlecomp": "c",
    "status": "temporary",
    "locationcomp": "fdsf",
    "category": "114",
    "month1comp": 4,
    "year1comp": 2004,
    "month2comp": 5,
    "year2comp": 1997,
    "desccomp": "dafds",
    "companydesc": "fdsaf",
    "_id": 0
}, {
    "company": "new",
    "schedule": "fulltime",
    "titlecomp": "b",
    "status": "temporary",
    "locationcomp": "fdsf",
    "category": "114",
    "month1comp": 4,
    "year1comp": 2015,
    "month2comp": 2,
    "year2comp": 2010,
    "desccomp": "dafds",
    "companydesc": "fdsaf",
    "_id": 1
}, {
    "company": "company name",
    "schedule": "fulltime",
    "titlecomp": "a",
    "status": "contract",
    "locationcomp": "sfdg",
    "category": "114",
    "month1comp": 2,
    "year1comp": 2015,
    "desccomp": "fdgsfdg",
    "companydesc": "fdgfdg",
    "_id":2
}]

template HTML

<div class="multiple-repeat-container" 
ng-repeat="item in expTest track by $index | orderBy:'-year1comp' "> ... </div>

Solution

  • You need to move tracking after the orderBy:

    <div class="multiple-repeat-container" ng-repeat="item in expTest | orderBy:'-year1comp' track by $index "> ... </div>
    

    The documentation states

    Note that the tracking expression must come last, after any filters, and the alias expression.