第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

像Python一樣按值對JS數(shù)組進(jìn)行排序

像Python一樣按值對JS數(shù)組進(jìn)行排序

湖上湖 2021-08-20 18:36:51
我如何在 JS 中對 python 代碼sort(array)或array.sort()(就地)等價的數(shù)組排序,如下例所示: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': ...},    ]
查看完整描述

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 }

]

這就是所有的人。


查看完整回答
反對 回復(fù) 2021-08-20
?
BIG陽

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]

查看完整回答
反對 回復(fù) 2021-08-20
  • 2 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號