Created
June 24, 2015 00:57
-
-
Save huyhong/9696c051c83a05bad2ef to your computer and use it in GitHub Desktop.
orderByPreferred AngularJS filter
This file contains hidden or 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
/** | |
* @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