MCPcopy
hub / github.com/stemkoski/stemkoski.github.com / sortBy

Function sortBy

MathBox/mathbox-bundle.js:39150–39181  ·  view source on GitHub ↗

* Creates an array of elements, sorted in ascending order by the results of * running each element in a collection through the callback. This method * performs a stable sort, that is, it will preserve the original sort order * of equal elements. The callback is bound to `thisArg` and

(collection, callback, thisArg)

Source from the content-addressed store, hash-verified

39148 * // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
39149 */
39150 function sortBy(collection, callback, thisArg) {
39151 var index = -1,
39152 isArr = isArray(callback),
39153 length = collection ? collection.length : 0,
39154 result = Array(typeof length == 'number' ? length : 0);
39155
39156 if (!isArr) {
39157 callback = lodash.createCallback(callback, thisArg, 3);
39158 }
39159 forEach(collection, function(value, key, collection) {
39160 var object = result[++index] = getObject();
39161 if (isArr) {
39162 object.criteria = map(callback, function(key) { return value[key]; });
39163 } else {
39164 (object.criteria = getArray())[0] = callback(value, key, collection);
39165 }
39166 object.index = index;
39167 object.value = value;
39168 });
39169
39170 length = result.length;
39171 result.sort(compareAscending);
39172 while (length--) {
39173 var object = result[length];
39174 result[length] = object.value;
39175 if (!isArr) {
39176 releaseArray(object.criteria);
39177 }
39178 releaseObject(object);
39179 }
39180 return result;
39181 }
39182
39183 /**
39184 * Converts the `collection` to an array.

Callers

nothing calls this directly

Calls 8

getArrayFunction · 0.85
releaseArrayFunction · 0.85
releaseObjectFunction · 0.85
isArrayFunction · 0.70
forEachFunction · 0.70
getObjectFunction · 0.70
mapFunction · 0.70
callbackFunction · 0.70

Tested by

no test coverage detected