Skip to content

Instantly share code, notes, and snippets.

@huyhong
Created June 24, 2015 00:57
Show Gist options
  • Save huyhong/9696c051c83a05bad2ef to your computer and use it in GitHub Desktop.
Save huyhong/9696c051c83a05bad2ef to your computer and use it in GitHub Desktop.
orderByPreferred AngularJS filter
/**
* @ngdoc filter
* @name orderByPreferred
* @memberof clrFilter
*
* @description
* Orders collection based on preferred values first
*
* @param {Object} input - Object you want to order
* @param {String} inputKey - Key of the object you want to prefer ordering by
* @param {Array} preferredValues - An array of values you want to order by, in order
*/
.filter('orderByPreferred', function() {
return function(input, inputKey, preferredValues) {
var result = []
for(var p = preferredValues.length - 1; p >= 0; p -= 1) {
for(var i = input.length - 1; i >= 0; i -= 1) {
if(preferredValues[p] == input[i][inputKey]) {
result.unshift(input[i])
input.splice(i, 1)
}
}
}
result = result.concat(input)
return result
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment