JavaScript 數組排序

JavaScript 數組排序

  • JS 數組方法
  • JS 數組迭代


JavaScript 數組排序

sort() 方法是最強大的數組方法之一。

數組排序

sort() 方法以字母順序對數組進行排序:

實例

<code>var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // 對 fruits 中的元素進行排序
/<code>

親自試一試

反轉數組

reverse() 方法反轉數組中的元素。

您可以使用它以降序對數組進行排序:

實例

<code>var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // 對 fruits 中的元素進行排序
fruits.reverse(); // 反轉元素順序
/<code>

親自試一試

數字排序

默認地,sort() 函數按照字符串順序對值進行排序。

該函數很適合字符串("Apple" 會排在 "Banana" 之前)。

不過,如果數字按照字符串來排序,則 "25" 大於 "100",因為 "2" 大於 "1"。

正因如此,sort() 方法在對數值排序時會產生不正確的結果。

我們通過一個比值函數來修正此問題:

實例

<code>var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
/<code>

親自試一試

使用相同的技巧對數組進行降序排序:

實例

<code>var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
/<code>

親自試一試

比值函數

比較函數的目的是定義另一種排序順序。

比較函數應該返回一個負,零或正值,這取決於參數:

<code>function(a, b){return a-b}/<code>

當 sort() 函數比較兩個值時,會將值發送到比較函數,並根據所返回的值(負、零或正值)對這些值進行排序。

實例:

當比較 40 和 100 時,sort() 方法會調用比較函數 function(40,100)。

該函數計算 40-100,然後返回 -60(負值)。

排序函數將把 40 排序為比 100 更低的值。

您可以使用下面的代碼片段來測試數值和字母排序:

<code><button>以字母順序排序/<button>
<button>以數字順序排序/<button>




/<code>

親自試一試

以隨機順序排序數組

實例

<code>var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return 0.5 - Math.random()});
/<code>

親自試一試

查找最高(或最低)的數組值

JavaScript 不提供查找數組中最大或最小數組值的內建函數。

不過,在對數組進行排序之後,您能夠使用索引來獲得最高或最低值。

升序排序:

實例

<code>var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});

// 現在 points[0] 包含最低值
// 而 points[points.length-1] 包含最高值
/<code>

親自試一試

降序排序:

實例

<code>var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});

// 現在 points[0] 包含最高值
// 而 points[points.length-1] 包含最低值
/<code>

親自試一試

如果您僅僅需要找到最高或最低值,對整個數組進行排序是效率極低的方法。

對數組使用 Math.max()

您可以使用 Math.max.apply 來查找數組中的最高值:

實例

<code>function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
/<code>

親自試一試

Math.max.apply([1, 2, 3]) 等於 Math.max(1, 2, 3)。

對數組使用 Math.min()

您可以使用 Math.min.apply 來查找數組中的最低值:

實例

<code>function myArrayMin(arr) {
return Math.min.apply(null, arr);
}
/<code>

親自試一試

Math.min.apply([1, 2, 3]) 等於 Math.min(1, 2, 3)。

我的 Min / Max JavaScript 方法

最快的解決方法是使用“自制”方法。

此函數遍歷數組,用找到的最高值與每個值進行比較:

實例(查找 Max)

<code>function myArrayMax(arr) { 

var len = arr.length
var max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
}
/<code>

親自試一試

此函數遍歷數組,用找到的最低值與每個值進行比較:

實例(查找 Min)

<code>function myArrayMin(arr) {
var len = arr.length
var min = Infinity;
while (len--) {
if (arr[len] < min) {
min = arr[len];
}
}
return min;
}
/<code>

親自試一試

排序對象數組

JavaScript 數組經常會包含對象:

實例

<code>var cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}];
/<code>

即使對象擁有不同數據類型的屬性,sort() 方法仍可用於對數組進行排序。

解決方法是通過比較函數來對比屬性值:

實例

<code>cars.sort(function(a, b){return a.year - b.year});/<code>

親自試一試

比較字符串屬性會稍複雜:

實例

<code>cars.sort(function(a, b){
\t var x = a.type.toLowerCase();
\t var y = b.type.toLowerCase();
\t if (x < y) {return -1;}
\t if (x > y) {return 1;}
\t return 0;
});
/<code>

親自試一試


分享到:


相關文章: