Skip to content

Instantly share code, notes, and snippets.

@JonCatmull
Created January 28, 2016 10:42
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 JonCatmull/b235151aabccc641599d to your computer and use it in GitHub Desktop.
Save JonCatmull/b235151aabccc641599d to your computer and use it in GitHub Desktop.
Angular filter - Move an array item to first position but preserve the rest of the lists order (only set up for basic array of either integers or strings)
(function() {
'use strict';
angular
.module('fcp')
.filter('itemFirst', itemFirst);
/** @ngInject */
function itemFirst($filter) {
var filter = function(input, property, key) {
var out = [];
if (typeof input !== 'undefined') {
if (typeof key !== 'undefined') {
var first = $filter('filter')(input,key,true);
var everythingElse = $filter('filter')(input,'!'+key,true);
out = first.concat(everythingElse);
} else {
out = input;
}
}
return out;
};
return filter;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment