In JavaScript, we can sort the elements of an array easily with a built-in method called the sort( ) function.
在JavaScript中,我们可以使用称为sort()函数的内置方法轻松地对数组的元素进行排序。
However, data types (string, number, and so on) can differ from one array to another. This means that using the sort( ) method alone is not always an appropriate solution.
但是,数据类型(字符串,数字等)在一个数组与另一个数组之间可能有所不同。 这意味着仅使用sort()方法并不总是合适的解决方案。
In this post, you will learn how to sort an array in JavaScript by using the sort( ) method for strings and numbers.
在本文中,您将学习如何通过对字符串和数字使用sort()方法在JavaScript中对数组进行排序。
字符串数组 (Array of Strings)
Let's start with strings:
让我们从字符串开始:
const teams = ['Real Madrid', 'Manchester Utd', 'Bayern Munich', 'Juventus'];
When we use the sort( ) method, elements will be sorted in ascending order (A to Z) by default:
当我们使用sort()方法时,默认情况下元素将按升序排序(从A到Z):
teams.sort();
// ['Bayern Munich', 'Juventus', 'Manchester Utd', 'Real Madrid']
If you prefer to sort the array in descending order, you need to use the reverse( ) method instead:
如果您希望按降序对数组进行排序,则需要使用reverse()方法:
teams.reverse();
// ['Real Madrid', 'Manchester Utd', 'Juventus', 'Bayern Munich']
数字数组 (Array of Numbers)
Sorting numbers is unfortunately not that simple. If we apply the sort method directly to a numbers array, we will see an unexpected result:
不幸的是,对数字进行排序并不是那么简单。 如果我们将sort方法直接应用于numbers数组,则会看到意外的结果:
const numbers = [3, 23, 12];
numbers.sort(); // --> 12, 23, 3
为什么sort()方法不适用于数字 (Why the sort( ) method isn't working for numbers)
Actually it is working, but this problem happens because JavaScript sorts numbers alphabetically. Let me explain this in detail.
实际上它是可行的,但是会发生此问题,因为JavaScript按字母顺序对数字进行排序。 让我详细解释一下。
Let's think of A=1, B=2, and C=3.
让我们考虑A = 1,B = 2和C = 3。
const myArray = ['C', 'BC', 'AB'];
myArray.sort(); // [AB, BC, C]
As an example, if we have three strings as C (3), BC (23) and AB(12), JavaScript will sort them as AB, BC, and C in an ascending order, which is alphabetically correct.
例如,如果我们有三个字符串,分别为C(3),BC(23)和AB(12),JavaScript会将它们按升序排序为AB,BC和C。
However, JavaScript will sort the numbers (alphabetically again) as 12, 23, and 3, which is incorrect.
但是,JavaScript会将数字按字母顺序再次排序为12、23和3,这是不正确的。
解决方案:比较功能 (Solution: The Compare Function)
Luckily, we can support the sort( ) method with a basic comparison function which will do the trick:
幸运的是,我们可以使用基本的比较功能来支持sort()方法,该方法可以完成此操作:
function(a, b) {return a - b}
The sort method, fortunately, can sort negative, zero, and positive values in the correct order. When the sort( ) method compares two values, it sends the values to our compare function and sorts the values according to the returned value.
幸运的是,排序方法可以按正确的顺序对负,零和正值进行排序。 当sort()方法比较两个值时,它将这些值发送到我们的compare函数,并根据返回的值对这些值进行排序。
- If the result is negative, a is sorted before b. 如果结果是否定的,则a排在b之前。
- If the result is positive, b is sorted before a. 如果结果是肯定的,则b在a之前排序。
- If the result is 0, nothing changes. 如果结果为0,则没有任何变化。
All we need to is using the compare function inside the sort( ) method:
我们需要做的就是在sort()方法中使用compare函数:
const numbers = [3, 23, 12];
numbers.sort(function(a, b){return a - b}); // --> 3, 12, 23
If we want to sort the numbers in descending order, this time we need to subtract the second parameter (b) from the first one (a):
如果要按降序对数字进行排序,这一次我们需要从第一个参数(a)中减去第二个参数(b):
const numbers = [3, 23, 12];
numbers.sort(function(a, b){return b - a}); // --> 23, 12, 3
结语 (Wrap Up)
So as we can see, sorting the elements of an array can be done easily in JavaScript with the sort( ) method, if we know how to use it correctly. I hope my post helps you to understand how to use the sort( ) method in JavaScript in the right way.
如我们所见,如果我们知道如何正确使用数组,可以使用sort()方法轻松地对数组的元素进行排序。 希望我的文章可以帮助您了解如何以正确的方式在JavaScript中使用sort()方法。
If you want to learn more about Web Development, feel free to visit my Youtube channel.
如果您想了解有关Web开发的更多信息,请随时访问我的Youtube频道 。
Thank you for reading!
感谢您的阅读!