javascript排序
The Javascript .sort method is one that I have struggled with for quite some time. I always found myself looking it up and fighting with it until one day I decided to save it in a file somewhere, and copy and paste it into my project whenever I needed to sort an array. This is clearly not the best way to code because you do not truly know how to use all of your tools.
我已经苦苦挣扎了很长时间的Javascript .sort方法。 我总是发现自己正在寻找它并与之抗争,直到有一天我决定将其保存在某个文件中,并在需要对数组进行排序时将其复制并粘贴到我的项目中。 显然,这不是最佳的编码方式,因为您并不真正知道如何使用所有工具。
Below we have an array of objects where each object holds a person’s first name, last name, and age. We can use the sort method to sort these people by any of these fields.
在下面,我们有一个对象数组,其中每个对象都包含一个人的名字,姓氏和年龄。 我们可以使用sort方法按照这些字段中的任何一个对这些人进行排序。
let people = [{ firstName: "Travis", lastName: "Prol", age: 24},{ firstName: "Christiana", lastName: "Alicante", age: 22},{ firstName: "Zach", lastName: "Love", age: 26},{ firstName: "Rachel", lastName: "Valocin", age: 25}]The .sort function allows the passing in of a callback function, which then accepts two parameters. These two parameters are two of the elements from the array.
.sort函数允许传入回调函数,该回调函数然后接受两个参数。 这两个参数是数组中的两个元素。
people.sort((a, b) => {})When .sort is called, it cycles through the array and returns a negative number, a positive number, or 0. These returns are based on which element (a or b) you want to appear before the other one. Let’s take a look at these returns:
调用.sort ,它将在数组中循环并返回一个负数,一个正数或0。这些返回值取决于要在另一个元素之前出现的元素( a或b )。 让我们看一下这些收益:
1. If return is < 0 ... a comes first2. If return is 0 ... nothing changes3. If return is > 0 ... b comes firstIf you are trying to sort by a field that is a number, this function will work by subtracting b.age from a.age. Make sure to return it!
如果您尝试按数字字段进行排序,则此功能将通过从a.age减去b.age来a.age 。 确保退货!
people.sort ((a, b) => {return a.age - b.age})This function properly sorts that array by everyone’s age!
此功能可以根据每个人的年龄正确排序该数组!
let people = [{ firstName: "Christiana", lastName: "Alicante", age: 22},{ firstName: "Travis", lastName: "Prol", age: 24},{ firstName: "Rachel", lastName: "Valocin", age: 25},{ firstName: "Zach", lastName: "Love", age: 26}]Let’s break down what the sort function did to the original array. Since Travis was first in the array, he becomes the a parameter, and since Christiana was second, she becomes the b parameter. When Christiana’s age of 22 is subtracted from Travis’ age of 24, 2 is returned. Since 2 is greater than 0, the sort function changes the order of the array, and puts the b element before the a. The function then reassigns what a and b are. Since Travis was moved to the second spot, he is now a, and Zach who is next in the array is now b. This continues until the entire array as been sorted.
让我们分解一下sort函数对原始数组的作用。 由于Travis在数组中排在第一位,因此他成为a参数,由于Christiana在数组中排在第二位,因此她成为b参数。 从特拉维斯的24岁减去克里斯蒂安娜的22岁时,将返回2。 由于图2是大于0,sort函数改变所述阵列的顺序,并把所述b元件的前a 。 然后,该函数重新分配a和b是什么。 自从Travis移至第二名以来,他现在a ,而数组中的下一个Zach现在是b 。 这一直持续到对整个数组进行排序为止。
If you try to do this with any other field, however, it will not work because if you try to subtract a string from a string, it does not return a number, and the order cannot change. In order to fix this, we need to explicitly tell the function what to return.
但是,如果尝试对任何其他字段执行此操作,则它将不起作用,因为如果您尝试从字符串中减去字符串,则该字符串不会返回数字,并且顺序也无法更改。 为了解决这个问题,我们需要明确告诉函数返回什么。
The Javascript comparison operators let us compare strings and will return booleans based on the comparison.
Javascript比较运算符使我们可以比较字符串,并根据比较结果返回布尔值。
We can see here in that in a Javascript console, that “apple” is less than “banana”. This is looks backwards of what you might expect it to be because B is the second letter of the alphabet (2), while A is the first (1).
我们在这里可以看到,在Javascript控制台中,“苹果”小于“香蕉”。 这是您所期望的倒向,因为B是字母的第二个字母(2),而A是第一个字母(1)。
Bringing this concept into our sort function, we can tell the function what value we want it to return based on the comparison of two strings.
将这个概念带入我们的排序函数中,我们可以基于两个字符串的比较,告诉函数我们希望它返回什么值。
people.sort((a, b) => { if (a.lastName.toLowerCase() < b.lastName.toLowerCase()) return -1; if (a.lastName.toLowerCase() > b.lastName.toLowerCase()) return 1; return 0;})Here, we are telling the function that if a < b, return -1 which, according to the rules of the function, will keep a first. When we return 1, b will be put first, and if they are the same, return 0, which will not change anything.
在这里,我们说的是功能,如果a < b return -1其中,根据功能的规则,将保持a第一。 当我们return 1 , b将放在第一位,如果它们相同,则return 0 ,这将不会有任何改变。
This function will sort the array correctly!
此函数将对数组正确排序!
let people = [{ firstName: "Christiana", lastName: "Alicante", age: 22},{ firstName: "Zach", lastName: "Love", age: 26},{ firstName: "Travis", lastName: "Prol", age: 24 },{ firstName: "Rachel", lastName: "Valocin", age: 25}]翻译自: https://medium.com/swlh/javascript-sort-method-dbde9095be60
javascript排序