Skip to content

Instantly share code, notes, and snippets.

@iksose
Forked from justinklemm/orderObjectBy.html
Created July 17, 2014 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iksose/b23acf90f4aadf8df118 to your computer and use it in GitHub Desktop.
Save iksose/b23acf90f4aadf8df118 to your computer and use it in GitHub Desktop.
<ul>
<li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }}</li>
</ul>
var app = angular.module('sample');
// inside your controller
$scope.items = {};
$scope.items['color-1'] = {color: "red"};
$scope.items['color-2'] = {color: "green"};
$scope.items['color-3'] = {color: "blue"};
// define filter on app
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
if(a[field] > b[field]) return 1;
if(a[field] < b[field]) return -1;
return 0;
});
if(reverse) filtered.reverse();
return filtered;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment