像Python一样按值对JS数组进行排序

我如何在 JS 中对 python 代码sort(array)或array.sort()(就地)等价的数组排序,如下例所示:


data = [

    { 'id': [2], 'other properties': ... },

    { 'id': [1,3,0,00,15], 'other properties': ... },

    { 'id': [1,0,0], 'other properties': ... },

    { 'id': [1,3,0,00,14], 'other properties': ... },

    { 'id': [1,3,0], 'other properties': ... },

]


data.sort(key = lambda e: e['id'] )



# output 

data == 

    [

        { 'id': [1, 0], 'other properties': ...},

        { 'id': [1, 0, 0], 'other properties': ...},

        { 'id': [1, 3, 0], 'other properties': ...},

        { 'id': [1, 3, 0, 0, 14], 'other properties': ...},

        { 'id': [1, 3, 0, 0, 15], 'other properties': ...},

        { 'id': [2] 'other properties': ...},

    ]


湖上湖
浏览 248回答 2
2回答

红颜莎娜

所以...我偶然发现自己想知道 Array 的排序方法是如何工作的,它对我来说确实看起来很不直观,但也许只是我自己。无论如何......考虑到我的python背景,我决定编写一个sorted尝试实现python排序逻辑的函数。该函数返回一个排序的数组而不修改原始数组。有两个可选参数以kwargs对象的形式作为第二个参数传递。可选参数有:reverse-当reverse为false(默认值)按升序排序,在情况下,它true则排序是下降。key- 是一个key生成器函数,items它使用原始位置处理生成键的数组,然后使用基元或元组之间的简单比较对键进行排序,类似于类似于 python 的比较对键数组进行排序后,根据键在排序输出中的位置排序返回原始数组。来源:function sorted(items, kwargs={}) {&nbsp; &nbsp; const key = kwargs.key === undefined ? x => x : kwargs.key;&nbsp; &nbsp; const reverse = kwargs.reverse === undefined ? false : kwargs.reverse;&nbsp; &nbsp; const sortKeys = items.map((item, pos) => [key(item), pos]);&nbsp; &nbsp; const comparator =&nbsp; &nbsp; &nbsp; &nbsp; Array.isArray(sortKeys[0][0])&nbsp; &nbsp; &nbsp; &nbsp; ? ((left, right) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var n = 0; n < Math.min(left.length, right.length); n++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const vLeft = left[n], vRight = right[n];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const order = vLeft == vRight ? 0 : (vLeft > vRight ? 1 : -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (order != 0) return order;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return left.length - right.length;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; : ((left, right) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const vLeft = left[0], vRight = right[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const order = vLeft == vRight ? 0 : (vLeft > vRight ? 1 : -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return order;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; sortKeys.sort(comparator);&nbsp; &nbsp; if (reverse) sortKeys.reverse();&nbsp; &nbsp; 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 = [&nbsp; &nbsp; { 'id': [2], 'other properties': null },&nbsp; &nbsp; { 'id': [1,3,0,00,-15], 'other properties': null },&nbsp; &nbsp; { 'id': [1,-3,0,00,15], 'other properties': null },&nbsp; &nbsp; { 'id': [1,-3,0,00,-15], 'other properties': null },&nbsp; &nbsp; { 'id': [1,0,0], 'other properties': null },&nbsp; &nbsp; { 'id': [1,3,0,00,14], 'other properties': null },&nbsp; &nbsp; { '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 [&nbsp; { id: [ 1, -3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, 15 ], 'other properties': null },&nbsp; { id: [ 1, 0, 0 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, 14 ], 'other properties': null },&nbsp; { id: [ 2 ], 'other properties': null }]JS asc [&nbsp; { id: [ 2 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, 15 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 1, 0, 0 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, 14 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0 ], 'other properties': null }]PY desc [&nbsp; { id: [ 2 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, 14 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0 ], 'other properties': null },&nbsp; { id: [ 1, 0, 0 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, 15 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, -15 ], 'other properties': null }]JS desc [&nbsp; { id: [ 1, 3, 0 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, 14 ], 'other properties': null },&nbsp; { id: [ 1, 0, 0 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 1, -3, 0, 0, 15 ], 'other properties': null },&nbsp; { id: [ 1, 3, 0, 0, -15 ], 'other properties': null },&nbsp; { id: [ 2 ], 'other properties': null }]这就是所有的人。

BIG阳

data.sort()将完全按照您的意愿进行排序,数组的直接级别data[3].sort()将更[1,3,0,00,14]改为[0,00,1,14,3]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript