红颜莎娜
所以...我偶然发现自己想知道 Array 的排序方法是如何工作的,它对我来说确实看起来很不直观,但也许只是我自己。无论如何......考虑到我的python背景,我决定编写一个sorted尝试实现python排序逻辑的函数。该函数返回一个排序的数组而不修改原始数组。有两个可选参数以kwargs对象的形式作为第二个参数传递。可选参数有:reverse-当reverse为false(默认值)按升序排序,在情况下,它true则排序是下降。key- 是一个key生成器函数,items它使用原始位置处理生成键的数组,然后使用基元或元组之间的简单比较对键进行排序,类似于类似于 python 的比较对键数组进行排序后,根据键在排序输出中的位置排序返回原始数组。来源: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())原始问题提供的数据的输出: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 }]这就是所有的人。