MCPcopy Create free account
hub / github.com/EricSimons/ionic-course / orderByFilter

Function orderByFilter

code/songhop/www/lib/angular/angular.js:17530–17610  ·  view source on GitHub ↗
($parse)

Source from the content-addressed store, hash-verified

17528 */
17529orderByFilter.$inject = ['$parse'];
17530function orderByFilter($parse) {
17531 return function(array, sortPredicate, reverseOrder) {
17532 if (!(isArrayLike(array))) return array;
17533 sortPredicate = isArray(sortPredicate) ? sortPredicate : [sortPredicate];
17534 if (sortPredicate.length === 0) { sortPredicate = ['+']; }
17535 sortPredicate = sortPredicate.map(function(predicate) {
17536 var descending = false, get = predicate || identity;
17537 if (isString(predicate)) {
17538 if ((predicate.charAt(0) == '+' || predicate.charAt(0) == '-')) {
17539 descending = predicate.charAt(0) == '-';
17540 predicate = predicate.substring(1);
17541 }
17542 if (predicate === '') {
17543 // Effectively no predicate was passed so we compare identity
17544 return reverseComparator(function(a, b) {
17545 return compare(a, b);
17546 }, descending);
17547 }
17548 get = $parse(predicate);
17549 if (get.constant) {
17550 var key = get();
17551 return reverseComparator(function(a, b) {
17552 return compare(a[key], b[key]);
17553 }, descending);
17554 }
17555 }
17556 return reverseComparator(function(a, b) {
17557 return compare(get(a),get(b));
17558 }, descending);
17559 });
17560 return slice.call(array).sort(reverseComparator(comparator, reverseOrder));
17561
17562 function comparator(o1, o2) {
17563 for (var i = 0; i < sortPredicate.length; i++) {
17564 var comp = sortPredicate[i](o1, o2);
17565 if (comp !== 0) return comp;
17566 }
17567 return 0;
17568 }
17569 function reverseComparator(comp, descending) {
17570 return descending
17571 ? function(a, b) {return comp(b,a);}
17572 : comp;
17573 }
17574 function compare(v1, v2) {
17575 var t1 = typeof v1;
17576 var t2 = typeof v2;
17577 // Prepare values for Abstract Relational Comparison
17578 // (http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5):
17579 // If the resulting values are identical, return 0 to prevent
17580 // incorrect re-ordering.
17581 if (t1 === t2 && t1 === "object") {
17582 // If types are both numbers, emulate abstract ToPrimitive() operation
17583 // in order to get primitive values suitable for comparison
17584 t1 = typeof (v1.valueOf ? v1 = v1.valueOf() : v1);
17585 t2 = typeof (v2.valueOf ? v2 = v2.valueOf() : v2);
17586 if (t1 === t2 && t1 === "object") {
17587 // Object.prototype.valueOf will return the original object, by

Callers

nothing calls this directly

Calls 4

isArrayLikeFunction · 0.70
isStringFunction · 0.70
reverseComparatorFunction · 0.70
compareFunction · 0.70

Tested by

no test coverage detected