This article mainly introduces the JS array sorting method, and analyzes the principles and implementation skills of javascript Array Bubble sorting and selection sorting in combination with examples, for more information about how to sort JS arrays, see the examples in this article. We will share this with you for your reference. The details are as follows:
Method 1. Bubble Sorting
Train of Thought: Compare the first element and the second element in the array in sequence. If the first element is greater than the second element, the switching position is required. Therefore, two functions are required: The switching position function and the comparison function.
The number of rounds is the length of the array.
Var arr = [2, 58, 49,26, 34]; function change (f, s) {var temp = arr [f]; arr [f] = arr [s]; arr [s] = temp;} for (var I = 0; iarr [j + 1]) {change (j, + j + 1 );}}} alert (arr );
Method 2: Select sorting
Find the minimum value from the array, throw it to the first value of the array, and then cyclically operate from the remaining Array
Var arr = [, 34]; function change () {if (arr. length = 1) {return arr;} var iMin = arr [0]; var index = 0; for (var I = 0; IThe above is the sample analysis content of the JS array sorting method. For more articles, please follow the PHP Chinese Network (www.php1.cn )!