In the previous article, we introduced several methods to detect whether all array elements meet the specified conditions. If you are interested, you can click on the link to check → "JS Array Learning: Determine whether the array elements all meet the given conditions. condition". This time we will talk about array sorting and introduce how to sort array elements in JavaScript.
The main content of today’s article is: sorting array elements according to certain rules. Without further ado, here are three methods for sorting array elements.
Method 1: Using for loop
When it comes to using for loop to sort arrays, bubble sorting comes to mind. Let’s take a look at the implementation code:
//每轮依次比较相邻两个数的大小,后面比前面小则交换 var b=0//设置用来调换位置的值 var a=[1,9,33,2,5,34,23,98,14]//冒泡排序 for(var i=0;i<a.length;i++){ for(var j=0;j<a.length;j++){ if(a[j]>a[j+1]){ b=a[j] a[j]=a[j+1] a[j+1]=b } } } console.log(a)
Output result:
The above code is sorted from small to large. If you want to sort from large to small, you can add and modify the judgment of the if statement:
if(a[j]>a[j+1]){ ... }
Method 2: Use the sort() method
The sort() method can sort array elements according to certain conditions.
Syntax: array.sort([function])
sort() method in two situations:
When the method When the parameter is empty, the elements in the array are sorted alphabetically from small to large.
The so-called alphabetical order is actually arranged according to the order of letters in the character encoding table. Each character has a unique number in the character table.
var a=['e','a','f','b','c','h','g','z','i'] a.sort(); //按字母顺序对元素进行排序 console.log(a);
Output result:
If the element is not a string, the sort() method attempts to convert the array elements into a string for comparison. Note that "40" will come before "5" when the numbers are sorted alphabetically.
var a=[1,2,40,4,5,50,6,7,8] a.sort(); //按字母顺序对元素进行排序 console.log(a);
Output result:
At this time, you need to call a function as a parameter, which is the second case below:
When the parameter is a function, the array elements will be sorted according to the rules specified by the function.
var a=[1,2,40,4,5,50,6,7,8] a.sort(function(a,b){ return a-b;//从小到大排序 //return b-a;//从大到小排序 }); console.log(a);
Output result:
When the prefix of the array elements is a number and the suffix is a string, if you want these elements to be To sort by numerical size, some modifications need to be made to the parameters in the anonymous function.
var a=['345px','23px','10px','1000px']; a.sort(function(a,b){ return parseInt(a)-parseInt(b);;//从小到大排序 //return parseInt(b)-parseInt(a);//从大到小排序 }); console.log(a);
Output result:
Method 3: Using the reverse() method
How to avoid changing array elements Sort from small to large (or large to small), but want to sort in reverse order? Then you can use the reverse() method.
reverse() method can reverse the order of array elements. This method does not require parameters, just array object.reverse()
.
var a=['345px','23px','10px','1000px']; a.reverse(); console.log(a);
Output result:
Description:
sort() and reverse() methods All operations are performed on the original array instead of creating a new array; therefore, the original array will be changed.
Okay, that’s all. If you need it, you can watch it: javascript video tutorial
The above is the detailed content of How to sort array elements according to certain rules in JS. For more information, please follow other related articles on the PHP Chinese website!