2 回答

TA貢獻(xiàn)1842條經(jīng)驗 獲得超13個贊
所以...我偶然發(fā)現(xiàn)自己想知道 Array 的排序方法是如何工作的,它對我來說確實看起來很不直觀,但也許只是我自己。
無論如何......考慮到我的python背景,我決定編寫一個sorted嘗試實現(xiàn)python排序邏輯的函數(shù)。
該函數(shù)返回一個排序的數(shù)組而不修改原始數(shù)組。
有兩個可選參數(shù)以kwargs對象的形式作為第二個參數(shù)傳遞。
可選參數(shù)有:
reverse-當(dāng)reverse為false(默認(rèn)值)按升序排序,在情況下,它true則排序是下降。
key- 是一個key生成器函數(shù),items它使用原始位置處理生成鍵的數(shù)組,然后使用基元或元組之間的簡單比較對鍵進(jìn)行排序,類似于類似于 python 的比較
對鍵數(shù)組進(jìn)行排序后,根據(jù)鍵在排序輸出中的位置排序返回原始數(shù)組。
來源:
function sorted(items, kwargs={}) {
const key = kwargs.key === undefined ? x => x : kwargs.key;
const reverse = kwargs.reverse === undefined ? false : kwargs.reverse;
const sortKeys = items.map((item, pos) => [key(item), pos]);
const comparator =
Array.isArray(sortKeys[0][0])
? ((left, right) => {
for (var n = 0; n < Math.min(left.length, right.length); n++) {
const vLeft = left[n], vRight = right[n];
const order = vLeft == vRight ? 0 : (vLeft > vRight ? 1 : -1);
if (order != 0) return order;
}
return left.length - right.length;
})
: ((left, right) => {
const vLeft = left[0], vRight = right[0];
const order = vLeft == vRight ? 0 : (vLeft > vRight ? 1 : -1);
return order;
});
sortKeys.sort(comparator);
if (reverse) sortKeys.reverse();
return sortKeys.map((order) => items[order[1]]);
}
用法示例:
console.log(sorted([-1, 9, -3, 4, -3, 6, 1, -7], {reverse: true}));
console.log(sorted([-1, 9, -3, 4, -3, 6, 1, -7]));
console.log(sorted(["U", "z", "Z", "f", "F", "a", "c", "d", "x"], {key: x => [x.toLowerCase(), x]}));
console.log(sorted(['-1', '9', '-3', '4', '-3', '6', '1', '-7', '11111']));
console.log(sorted(['-1', '9', '-3', '4', '-3', '6', '1', '-7', '11111'], {key: x => parseFloat(x)}));
data = [
{ 'id': [2], 'other properties': null },
{ 'id': [1,3,0,00,-15], 'other properties': null },
{ 'id': [1,-3,0,00,15], 'other properties': null },
{ 'id': [1,-3,0,00,-15], 'other properties': null },
{ 'id': [1,0,0], 'other properties': null },
{ 'id': [1,3,0,00,14], 'other properties': null },
{ 'id': [1,3,0], 'other properties': null },
]
console.log("PY asc", sorted(data, {key: x=>x.id}))
console.log("JS asc", [...data].sort(x=>x.id))
console.log("PY desc", sorted(data, {key: x=>x.id, reverse: true}))
console.log("JS desc", [...data].sort(x=>x.id).reverse())
原始問題提供的數(shù)據(jù)的輸出:
PY asc [
{ id: [ 1, -3, 0, 0, -15 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, 15 ], 'other properties': null },
{ id: [ 1, 0, 0 ], 'other properties': null },
{ id: [ 1, 3, 0 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, -15 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, 14 ], 'other properties': null },
{ id: [ 2 ], 'other properties': null }
]
JS asc [
{ id: [ 2 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, -15 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, 15 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, -15 ], 'other properties': null },
{ id: [ 1, 0, 0 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, 14 ], 'other properties': null },
{ id: [ 1, 3, 0 ], 'other properties': null }
]
PY desc [
{ id: [ 2 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, 14 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, -15 ], 'other properties': null },
{ id: [ 1, 3, 0 ], 'other properties': null },
{ id: [ 1, 0, 0 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, 15 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, -15 ], 'other properties': null }
]
JS desc [
{ id: [ 1, 3, 0 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, 14 ], 'other properties': null },
{ id: [ 1, 0, 0 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, -15 ], 'other properties': null },
{ id: [ 1, -3, 0, 0, 15 ], 'other properties': null },
{ id: [ 1, 3, 0, 0, -15 ], 'other properties': null },
{ id: [ 2 ], 'other properties': null }
]
這就是所有的人。

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊
data.sort()
將完全按照您的意愿進(jìn)行
排序,數(shù)組的直接級別data[3].sort()
將更[1,3,0,00,14]
改為[0,00,1,14,3]
添加回答
舉報