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

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

對具有 Object 數(shù)組的 JavaScript 數(shù)組進(jìn)行排序

對具有 Object 數(shù)組的 JavaScript 數(shù)組進(jìn)行排序

撒科打諢 2022-05-26 15:39:18
您能否建議我根據(jù)部分名稱按優(yōu)先級對以下數(shù)組進(jìn)行排序的最佳方法。我更擔(dān)心時間復(fù)雜度,因?yàn)槲业臄?shù)組實(shí)際上包含 100 000 條記錄。如果有更好的存儲方式,我也可以更改數(shù)組結(jié)構(gòu)[{    id: 'field1',    sections: [{        name: 'Top_Section',        priority: 3      },      {        name: 'Bottom_Section',        priority: 3      }    ]  },  {    id: 'field2',    sections: [{        name: 'Top_Section',        priority: 2      },      {        name: 'Bottom_Section',        priority: 4      }    ]  },  {    id: 'field3',    sections: [{        name: 'Top_Section',        priority: 1      },      {        name: 'Bottom_Section',        priority: 1      }    ]  },  {    id: 'field4',    sections: [{        name: 'Top_Section',        priority: 4      },      {        name: 'Bottom_Section',        priority: 2      }    ]  }];就像我想根據(jù) Top_Section 對優(yōu)先級進(jìn)行排序一樣,所以我的預(yù)期輸出應(yīng)該如下所示,因?yàn)?field3 的優(yōu)先級為 1,而 field2 的優(yōu)先級為 2,依此類推。[  {    id: 'field3',    sections: [      { name: 'Top_Section', priority: 1 },      { name: 'Bottom_Section', priority: 1 }    ]  },  {    id: 'field2',    sections: [      { name: 'Top_Section', priority: 2 },      { name: 'Bottom_Section', priority: 4 }    ]  },  {    id: 'field1',    sections: [      { name: 'Top_Section', priority: 3 },      { name: 'Bottom_Section', priority: 3 }    ]  },  {    id: 'field4',    sections: [      { name: 'Top_Section', priority: 4 },      { name: 'Bottom_Section', priority: 2 }    ]  }];
查看完整描述

2 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個贊

我在這里假設(shè)“Top_Section”總是在sections 數(shù)組的第一個位置。


我還假設(shè)只有兩種優(yōu)先級類型:“Top_Section”和“Bottom_Section”


let list = [{

    id: 'field1',

    sections: [{

        name: 'Top_Section',

        priority: 3

      },

      {

        name: 'Bottom_Section',

        priority: 3

      }

    ]

  },

  {

    id: 'field2',

    sections: [{

        name: 'Top_Section',

        priority: 2

      },

      {

        name: 'Bottom_Section',

        priority: 4

      }

    ]

  },

  {

    id: 'field3',

    sections: [{

        name: 'Top_Section',

        priority: 1

      },

      {

        name: 'Bottom_Section',

        priority: 1

      }

    ]

  },

  {

    id: 'field4',

    sections: [{

        name: 'Top_Section',

        priority: 4

      },

      {

        name: 'Bottom_Section',

        priority: 2

      }

    ]

  }

];


function sortBy(priorityName) {

  let priorityPosition = (priorityName == 'Top_Section') ? 0 : 1;

  

  return (a, b) => {

    return a['sections'][priorityPosition].priority - b['sections'][priorityPosition].priority;

  }

}


console.log( list.sort(sortBy('Top_Section')) );


查看完整回答
反對 回復(fù) 2022-05-26
?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個贊

讓我們創(chuàng)建一個比較器


function compare(a, b) {

    var sumA = 0;

    var sumB = 0;

    for (var section of a.sections) sumA += section.priority;

    for (var section of b.sections) sumB += seciton.priority;

    return sumB - sumA;

}


arr.sort(compare);

如果第一個參數(shù)較大,比較器返回正數(shù),如果第二個參數(shù)較大,則比較器返回負(fù)數(shù),如果它們相等,則返回 0。我假設(shè)優(yōu)先級總和的數(shù)值越小,項目越大。


查看完整回答
反對 回復(fù) 2022-05-26
  • 2 回答
  • 0 關(guān)注
  • 233 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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