js中Array.sort()利用零值多維排序
多維排序
要求:先按數(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()實現(xiàn)原理:
1. 插入排序

從左往右遍歷數(shù)組,每次將遍歷的項插入到前面的已經(jīng)排序好的有序數(shù)組中,通過構(gòu)建有序序列,對于未排序數(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個值,和前面已排好的i-1個值進行比較
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. 快速排序

基本思想
- 在數(shù)據(jù)集之中,選擇一個元素作為"基準(zhǔn)"(pivot)。
- 所有小于"基準(zhǔn)"的元素,都移到"基準(zhǔn)"的左邊;所有大于"基準(zhǔn)"的元素,都移到"基準(zhǔn)"的右邊。
- 對"基準(zhǔn)"左邊和右邊的兩個子集,不斷重復(fù)第一步和第二步,直到所有子集只剩下一個元素為止。
算法步驟
定義一個quickSort函數(shù)
- 檢查數(shù)組的元素個數(shù),如果小于等于1,就返回。
- 選擇"基準(zhǔn)"(pivot),并將其與原數(shù)組分離,再定義兩個空數(shù)組,用來存放一左一右的兩個子集。
- 開始遍歷數(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];
// 定義左右兩個空數(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ù)組長度小于等于 10 的用插入排序InsertionSort,比10大的數(shù)組則使用快速排序 QuickSort
參考文檔: Array.sort()方法和實現(xiàn)機制
到此這篇關(guān)于js中Array.sort()利用零值多維排序的文章就介紹到這了,更多相關(guān)js Array.sort()多維排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js中document.referrer實現(xiàn)移動端返回上一頁
本文主要介紹了document.referrer實現(xiàn)移動端返回上一頁的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
js 利用image對象實現(xiàn)圖片的預(yù)加載提高訪問速度
我們來學(xué)習(xí)一種名為圖像預(yù)裝載(image preloading)的小技巧來提高圖像訪問速度,一些瀏覽器試圖通過在本地緩存中保存這些圖片來解決此問題,感興趣的朋友可以了解下2013-03-03
js實現(xiàn)iframe自動自適應(yīng)高度的方法
這篇文章主要介紹了js實現(xiàn)iframe自動自適應(yīng)高度的方法,涉及javascript操作iframe框架的技巧,非常具有實用價值,需要的朋友可以參考下2015-02-02

