Home > Web Front-end > JS Tutorial > How to sort array elements according to certain rules in JS

How to sort array elements according to certain rules in JS

青灯夜游
Release: 2021-08-27 14:37:54
Original
7550 people have browsed it

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)
Copy after login

Output result:

How to sort array elements according to certain rules in JS

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]){
...
}
Copy after login

How to sort array elements according to certain rules in JS

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=[&#39;e&#39;,&#39;a&#39;,&#39;f&#39;,&#39;b&#39;,&#39;c&#39;,&#39;h&#39;,&#39;g&#39;,&#39;z&#39;,&#39;i&#39;]
a.sort();  //按字母顺序对元素进行排序
console.log(a);
Copy after login

Output result:

How to sort array elements according to certain rules in JS

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);
Copy after login

Output result:

How to sort array elements according to certain rules in JS

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);
Copy after login

Output result:

How to sort array elements according to certain rules in JS

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=[&#39;345px&#39;,&#39;23px&#39;,&#39;10px&#39;,&#39;1000px&#39;];
a.sort(function(a,b){
     return parseInt(a)-parseInt(b);;//从小到大排序
     //return parseInt(b)-parseInt(a);//从大到小排序
});
console.log(a);
Copy after login

Output result:

How to sort array elements according to certain rules in JS

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=[&#39;345px&#39;,&#39;23px&#39;,&#39;10px&#39;,&#39;1000px&#39;];
a.reverse();
console.log(a);
Copy after login

Output result:

How to sort array elements according to certain rules in JS

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template