When sorting arrays in JavaScript, I summarized three methods: sort sorting, bubble sorting and selection sorting.
var arr = [1,22,3,4,5,6,7,8,9,10];
sort sorting:
It is implemented using the method sort() in the array.
<span style="color: #ff0000">function <strong><code class="js plain">systemSort
(arr)
##{
arr.sort(
##{
return a - b; }
## );## }
Bubble sorting
: FUNCTION Selectsort1 (ARR) {
var temp = arr[j];
## arr[j] = arr[j+1 ];# arr[j+1] = temp;
## }
#Selection sort:
The selection sort method is to use the first data of the array as the maximum or minimum value, and then output an ordered array through a comparison loop.
## For (var I = 0; I & LT; Arrr.Length-1) {//// /Comparison rounds of the outer loop
## Number
counter (ARR [J] & GT; ARR [J+1]) { v varte = arr[j];
# arr[j +1] = temp;
## ##
The above is the detailed content of Detailed explanation of array sorting in js. For more information, please follow other related articles on the PHP Chinese website!