最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js中Array.sort()利用零值多維排序

 更新時(shí)間:2023年05月19日 09:24:59   作者:JohnsonW  
本文主要介紹了js中Array.sort()利用零值多維排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

多維排序

要求:先按數(shù)字順序依次倒序排列,最后按字母排序

let arr = ["apple 55 33 11", "orange 55 40 18", "banana 33 40 15", "watermeion 33 40 17", "peach 33 40 17"]
const compareName = (a, b) => a > b ? 1 : -1
let result = arr.map(item => item.split(' ')).sort((a, b) => {
  return (+b[1]) - (+a[1]) || (+b[2]) - (+a[2]) || (+b[3]) - (+a[3]) || compareName(a[0], b[0])
})
console.log(result)
// [
//   [ 'orange', '55', '40', '18' ],
//   [ 'apple', '55', '33', '11' ],
//   [ 'peach', '33', '40', '17' ],
//   [ 'watermeion', '33', '40', '17' ],
//   [ 'banana', '33', '40', '15' ]
// ]

Array.sort()實(shí)現(xiàn)原理:

1. 插入排序

insertionSort.gif

從左往右遍歷數(shù)組,每次將遍歷的項(xiàng)插入到前面的已經(jīng)排序好的有序數(shù)組中,通過(guò)構(gòu)建有序序列,對(duì)于未排序數(shù)據(jù),在已排序序列中從后向前掃描,找到相應(yīng)位置并插入。

const insertionSort = function(callback) {
  arr = this
  const len = arr.length
  let preIndex, current
  for (let i = 1; i < len; i++) {
    preIndex = i - 1
    current = arr[i]
    while(preIndex >= 0 && (callback(current, arr[preIndex]) < 0)){
      //callback:將第i個(gè)值,和前面已排好的i-1個(gè)值進(jìn)行比較
      arr[preIndex + 1] = arr[preIndex]
      preIndex--
    }
    arr[preIndex + 1] = current
  }
  return arr
}
Array.prototype.insertionSort = insertionSort

調(diào)用

let arr = ["apple 55 33 11", "orange 55 40 18", "banana 33 40 15", "watermeion 33 40 17", "peach 33 40 17"]
const compareName = (a, b) => a > b ? 1 : -1
arr.map(item => item.split(' ')).insertionSort((a, b) => {
  return (+b[1]) - (+a[1]) || (+b[2]) - (+a[2]) || (+b[3]) - (+a[3]) || compareName(a[0], b[0])
})
console.log(arr)
// [
//   [ 'orange', '55', '40', '18' ],
//   [ 'apple', '55', '33', '11' ],
//   [ 'peach', '33', '40', '17' ],
//   [ 'watermeion', '33', '40', '17' ],
//   [ 'banana', '33', '40', '15' ]
// ]

2. 快速排序

quickSort.png

基本思想

  • 在數(shù)據(jù)集之中,選擇一個(gè)元素作為"基準(zhǔn)"(pivot)。
  • 所有小于"基準(zhǔn)"的元素,都移到"基準(zhǔn)"的左邊;所有大于"基準(zhǔn)"的元素,都移到"基準(zhǔn)"的右邊。
  • 對(duì)"基準(zhǔn)"左邊和右邊的兩個(gè)子集,不斷重復(fù)第一步和第二步,直到所有子集只剩下一個(gè)元素為止。

算法步驟

定義一個(gè)quickSort函數(shù)

  • 檢查數(shù)組的元素個(gè)數(shù),如果小于等于1,就返回。
  • 選擇"基準(zhǔn)"(pivot),并將其與原數(shù)組分離,再定義兩個(gè)空數(shù)組,用來(lái)存放一左一右的兩個(gè)子集。
  • 開(kāi)始遍歷數(shù)組,小于"基準(zhǔn)"的元素放入左邊的子集,大于基準(zhǔn)的元素放入右邊的子集。
  • 遞歸重復(fù)
const quickSort = function (callback) {
  let arr = this
  if (arr.length <= 1) { return arr; }
  // 獲取基準(zhǔn)值得索引
  let pivotIndex = Math.floor(arr.length / 2);
  // 獲得索引值
  let pivot = arr.splice(pivotIndex, 1)[0];
  // 定義左右兩個(gè)空數(shù)組
  let left = [];
  let right = [];
  // 分組
  for (let i = 0; i < arr.length; i++) {
    if (callback(arr[i], pivot) < 0) {
      left.push(arr[i]);
    } else {
      right.push(arr[i]);
    }
  }
  // 遞歸
  return [...left].quickSort(callback).concat([pivot], [...right].quickSort(callback));
};
Array.prototype.quickSort = quickSort

數(shù)組長(zhǎng)度小于等于 10 的用插入排序InsertionSort,比10大的數(shù)組則使用快速排序 QuickSort

參考文檔: Array.sort()方法和實(shí)現(xiàn)機(jī)制

到此這篇關(guān)于js中Array.sort()利用零值多維排序的文章就介紹到這了,更多相關(guān)js Array.sort()多維排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

双牌县| 亚东县| 土默特右旗| 土默特左旗| 牙克石市| 东海县| 景泰县| 伽师县| 呼伦贝尔市| 永胜县| 建德市| 涟源市| 政和县| 苏尼特右旗| 大化| 张家港市| 绍兴市| 达拉特旗| 平乡县| 汉中市| 太白县| 新昌县| 资源县| 沙雅县| 临高县| 德保县| 马鞍山市| 澄迈县| 静安区| 西林县| 临洮县| 刚察县| 贵定县| 界首市| 汉川市| 巴彦淖尔市| 鄄城县| 乳源| 环江| 福州市| 东阳市|