Created
February 3, 2015 09:33
-
-
Save samuelbeek/d15ffa5b5b3b7c899bb8 to your computer and use it in GitHub Desktop.
Order Arrays in AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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