1 | module.exports = function(list, iteratee) {
|
2 | if (typeof iteratee !== 'function') {
|
3 | var fieldName = iteratee;
|
4 | iteratee = undefined;
|
5 | }
|
6 |
|
7 | return list.map(function(value, index) {
|
8 | return {
|
9 | value: value,
|
10 | index: index,
|
11 | criteria: iteratee? iteratee(value, index, list): value[fieldName]
|
12 | };
|
13 | }).sort(function(left, right) {
|
14 | var a = left.criteria;
|
15 | var b = right.criteria;
|
16 | if (a !== b) {
|
17 | if (a > b || a === void 0) return 1;
|
18 | if (a < b || b === void 0) return -1;
|
19 | }
|
20 | return left.index - right.index;
|
21 | }).map(function (item) {
|
22 | return item.value;
|
23 | });
|
24 | };
|