Custom order using orderBy in ng-repeat Custom order using orderBy in ng-repeat angularjs angularjs

Custom order using orderBy in ng-repeat


Hi you can create custom sort filter please see here http://jsbin.com/lizesuli/1/edit

html:

  <p ng-repeat="s in students |customSorter:'class'">{{s.name}} - {{s.class}} </p>      </div>

angularjs filter:

app.filter('customSorter', function() {  function CustomOrder(item) {    switch(item) {      case 'A_Class':        return 2;      case 'B_Class':        return 1;      case 'C_Class':        return 3;    }    }  return function(items, field) {    var filtered = [];    angular.forEach(items, function(item) {      filtered.push(item);    });    filtered.sort(function (a, b) {          return (CustomOrder(a.class) > CustomOrder(b.class) ? 1 : -1);    });    return filtered;  };});


Know this is old but may come in handy for others...

You could also create a simple custom sort function. "Not quite a filter":

$scope.customOrder = function (item) {        switch (item) {            case 'A_Class':                return 2;            case 'B_Class':                return 1;            case 'C_Class':                return 3;        }    };

And then use like you wanted to:

<table><tr ng-repeat="student in students | orderBy:customOrder">...</tr>


to set the orderBy as a property of the objects just quote that property name within the markup:

ng-repeat="student in students |orderBy:'name' | orderBy:'class'"

DEMO