Skip to content

Instantly share code, notes, and snippets.

@samuelbeek
Created February 3, 2015 09:33
Show Gist options
  • Save samuelbeek/d15ffa5b5b3b7c899bb8 to your computer and use it in GitHub Desktop.
Save samuelbeek/d15ffa5b5b3b7c899bb8 to your computer and use it in GitHub Desktop.
Order Arrays in AngularJS
// order an array
// to use: <div ng-repeat="thing in things | orderTimeObjectBy:'order'">
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (parseInt(a[field]) >parseInt(b[field]) ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment