MCPcopy Index your code
hub / github.com/keepfool/vue-tutorials / orderBy

Function orderBy

01.GettingStarted/js/vue.js:9680–9728  ·  view source on GitHub ↗

* Filter filter for arrays * * @param {String|Array |Function} ...sortKeys * @param {Number} [order]

(arr)

Source from the content-addressed store, hash-verified

9678 */
9679
9680 function orderBy(arr) {
9681 var comparator = null;
9682 var sortKeys = undefined;
9683 arr = convertArray(arr);
9684
9685 // determine order (last argument)
9686 var args = toArray(arguments, 1);
9687 var order = args[args.length - 1];
9688 if (typeof order === 'number') {
9689 order = order < 0 ? -1 : 1;
9690 args = args.length > 1 ? args.slice(0, -1) : args;
9691 } else {
9692 order = 1;
9693 }
9694
9695 // determine sortKeys & comparator
9696 var firstArg = args[0];
9697 if (!firstArg) {
9698 return arr;
9699 } else if (typeof firstArg === 'function') {
9700 // custom comparator
9701 comparator = function (a, b) {
9702 return firstArg(a, b) * order;
9703 };
9704 } else {
9705 // string keys. flatten first
9706 sortKeys = Array.prototype.concat.apply([], args);
9707 comparator = function (a, b, i) {
9708 i = i || 0;
9709 return i >= sortKeys.length - 1 ? baseCompare(a, b, i) : baseCompare(a, b, i) || comparator(a, b, i + 1);
9710 };
9711 }
9712
9713 function baseCompare(a, b, sortKeyIndex) {
9714 var sortKey = sortKeys[sortKeyIndex];
9715 if (sortKey) {
9716 if (sortKey !== '$key') {
9717 if (isObject(a) && '$value' in a) a = a.$value;
9718 if (isObject(b) && '$value' in b) b = b.$value;
9719 }
9720 a = isObject(a) ? getPath(a, sortKey) : a;
9721 b = isObject(b) ? getPath(b, sortKey) : b;
9722 }
9723 return a === b ? 0 : a > b ? order : -order;
9724 }
9725
9726 // sort on a copy to avoid mutating original array
9727 return arr.slice().sort(comparator);
9728 }
9729
9730 /**
9731 * String contain helper

Callers

nothing calls this directly

Calls 2

toArrayFunction · 0.70
baseCompareFunction · 0.70

Tested by

no test coverage detected