[英]Modifying sort function in JavaScript
有人可以解釋以下代碼:
var values = [213, 16, 2058, 54, 10, 1965, 57, 9];
values.sort(function(value1,value2){ return value2 - value1; });
我無法理解如何從數組值中加載value1和value2,以及它如何顛倒排序結果。
排序功能執行以下操作:
return value2 - value1;
讓我們對其進行詳細說明,以便我們了解發生了什么:
var values = [213, 16, 2058, 54, 10, 1965, 57, 9];
values.sort(function(value1,value2){
console.log(value2 + ' - ' + value1 + ' = ' + (value2 - value1) + ' | (' + (value2 - value1 > 0 ? 'positive | ' + value2 + ' should be before ' + value1 : 'negative | ' + value2 + ' should be after ' + value1) + ')');
return value2 - value1;
});
輸出:
16 - 213 = -197 | (negative | 16 should be after 213)
2058 - 16 = 2042 | (positive | 2058 should be before 16)
2058 - 213 = 1845 | (positive | 2058 should be before 213)
54 - 16 = 38 | (positive | 54 should be before 16)
54 - 213 = -159 | (negative | 54 should be after 213)
10 - 16 = -6 | (negative | 10 should be after 16)
1965 - 10 = 1955 | (positive | 1965 should be before 10)
1965 - 16 = 1949 | (positive | 1965 should be before 16)
1965 - 54 = 1911 | (positive | 1965 should be before 54)
1965 - 213 = 1752 | (positive | 1965 should be before 213)
1965 - 2058 = -93 | (negative | 1965 should be after 2058)
57 - 10 = 47 | (positive | 57 should be before 10)
57 - 16 = 41 | (positive | 57 should be before 16)
57 - 54 = 3 | (positive | 57 should be before 54)
57 - 213 = -156 | (negative | 57 should be after 213)
9 - 10 = -1 | (negative | 9 should be after 10)
如果對所有值都執行以下操作,您會注意到,如果value2
大於value1
則結果將始終為正,並且將在數組中向前移動-本質上是對數組進行反向排序。
value1
和value2
是您創建的函數的變量。 該函數將傳遞給sort
函數,后者負責對數組進行排序。
sort
函數通過在成對的元素上調用提供的函數來算出元素應按什么順序排列。 根據結果(負數或正數), sort
功能對它們進行sort
。
在這種情況下,如果value2
大於value1
,則返回正數,這意味着它將按從大到小的順序排序。
Array#sort
函數反復調用您為其提供的回調(稱為比較器 ),以比較數組中的兩個值。 它在對數組進行排序時根據需要執行此操作。 因此排序邏輯在Array#sort
,但比較邏輯在您提供的比較器函數中。
比較器應返回以下三個值之一:
0
< 0
> 0
如果第一個參數“大於”第二個參數 規范沒有規定sort
調用比較器的次數以及這些調用的順序。
您的特定比較器正在比較數字,因此非常簡單:它只返回value2 - value1
。 這意味着如果value1
小於value2
,則比較器將返回正數;否則,比較器將返回正數。 如果value1
等於value2
,則比較器將返回0
;否則,比較器將返回0
。 如果value1
大於value2
,則返回-1
。 因此,此特定比較器以相反的數字順序排序 (最大的數字在前)。
對於幾乎所有排序功能,您都需要在某個時間點比較兩個值,以確定哪個較大和哪個較小。 您進行多少次比較顯然取決於排序算法的效率。
您可以使用javascript和許多其他語言來提供此行為。 您在sort()
提供的任何函數都稱為比較器,預期該函數將返回以下三個值之一:
JavaScript中的數組排序是通過方法array.sort()完成的,該方法可能被人們誤解為低估。 雖然本身調用sort()只是按字典順序(又稱字母順序)對數組進行排序,但是一旦超出表面,天空實際上就是極限。 按字典順序對數組排序
按字典順序(又稱“字母順序”或字典順序)對數組進行排序很容易。 只需調用array.sort(),而無需傳入任何參數:
//Sort alphabetically and ascending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]
請注意,順序是遞增的。 為了使其下降,最簡單的方法是結合使用另一個Array方法array.reverse()的幫助:
//Sort alphabetically and descending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort()
myarray.reverse() //Array now becomes ["Bully", "Bob", "Amy"]
現在,在您開始感到自在之前,請考慮如果我們在由數字組成的數組上調用array.sort()會發生什么:
var myarray=[7, 40, 300]
myarray.sort() //Array now becomes [300,40,7]
盡管從字面上看7在數值上小於40或300,但它更大,因此7出現在排序數組的最右邊。 請記住,默認情況下,array.sort()以字典順序對其元素進行排序。
就基本用法而言,它與array.sort()一起使用。 但是,這種方法不僅僅具有令人眼前一亮的效果。 Array.sort()接受函數引用形式的可選參數,該參數幾乎使您可以根據任何自定義條件對數組進行排序,例如按數字對數組進行排序或對其進行隨機排序(隨機化其元素的順序)。 將函數引用傳遞給array.sort()如前所述,array.sort()接受函數引用形式的可選參數(我們稱其為sortfunction)。 該函數的格式如下:
array.sort(sortfunction)
function sortfunction(a, b){
//Compare "a" and "b" in some fashion, and return -1, 0, or 1
}
將此類函數傳遞給array.sort()時,將基於每對元素“ a”和“ b”之間的關系以及函數的返回值對數組元素進行排序。 三個可能的返回數字是:<0(小於0),0或> 0(大於0):
Less than 0: Sort "a" to be a lower index than "b" Zero: "a" and "b" should be considered equal, and no sorting performed. Greater than 0: Sort "b" to be a lower index than "a".
例如,要對數組進行數字排序並升序,函數的主體應如下所示:
function sortfunction(a, b){
return (a - b) //causes an array to be sorted numerically and ascending
}
在下面的更多內容。 按數字順序對數組排序
要按數字順序對數組進行排序,只需將自定義排序函數傳遞到array.sort()中,該函數將返回“ a”和“ b”之間的差,這兩個參數間接/自動地饋入函數:
//Sort numerically and ascending:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return a - b}) //Array now becomes [7, 8, 25, 41]
這之所以可行,是因為無論何時“ a”小於“ b”,都將返回負值,這將導致較小的元素始終出現在較大的元素的左側,換句話說,是遞增的。
對數組進行數字排序,但降序並沒有太大區別,只需要反轉兩個操作數“ a”和“ b”:
//Sort numerically and descending:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return b - a}) //Array now becomes [41, 25, 8, 71]
改組(隨機化)數組的順序
為了隨機化數組中元素的順序,我們需要的是sort函數的主體,以返回隨機地<0、0或> 0的數字,而與“ a”和“ b”之間的關系無關。 下面將解決這個問題:
//Randomize the order of the array:
var myarray=[25, 8, "George", "John"]
myarray.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
如您所見,array.sort()的內容比許多人想象的要多。 實際上,您甚至可以對數組進行排序,這些數組不僅包含原始值,還包含具有屬性的對象。 接下來讓我們看看。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.