[英]JavaScript: Sort an array of objects by a numeric property in each object
I have a hard time understanding how to use the array.sort() method to sort an array of objects based on a numeric value found in every object. 我很难理解如何使用array.sort()方法根据每个对象中的数值对对象数组进行排序。 Essentially I have a scenario of the following form:
基本上我有以下形式的场景:
var myarray = []
myarray.push({name: "alex", age: 8})
myarray.push({name: "ben", age: 57})
myarray.push({name: "dan", age: 32})
The initial order of the result would be "alex, ben, dan". 结果的初始顺序是“alex,ben,dan”。 Now I want to sort this array by age, so the oldest people are first on the list.
现在我想按年龄对这个数组进行排序,所以最老的人是第一个在列表中。 After sorting the order should be "ben, dan, alex".
排序后的顺序应该是“ben,dan,alex”。 How do I achieve this in the simplest way possible?
我如何以最简单的方式实现这一目标?
You can use destructuring assignment and the .sort
method like so: 你可以使用解构赋值和
.sort
方法,如下所示:
var myarray = [] myarray.push({name: "alex", age: 8}) myarray.push({name: "ben", age: 57}) myarray.push({name: "dan", age: 32}); var res = myarray.sort(({age:a}, {age:b}) => ba); console.log(res);
Or, if you don't feel comfortable with destructing, you can use regular dot notation to access the age
property: 或者,如果您对破坏感觉不舒服,可以使用常规点表示法来访问
age
属性:
var myarray = [] myarray.push({name: "alex", age: 8}) myarray.push({name: "ben", age: 57}) myarray.push({name: "dan", age: 32}); var res = myarray.sort((a, b) => b.age-a.age); console.log(res);
The way .sort
works is defined by what you return from the callback function you pass in. If you return: .sort
工作方式由您从传入的回调函数返回的内容定义。如果返回:
<= -1
then a
will come before b
. <= -1
然后a
将在b
之前出现。 0
then keep a
and b
in the same positions 0
然后将a
和b
保持在相同的位置 >= 1
then b
will come before a
>= 1
然后b
将在a
之前到来 Thus, by calculating the difference between the two ages, this "naturally" gives the correct values to properly sort your array. 因此,通过计算两个年龄之间的差异,这个“自然”给出了正确的值来正确排序你的数组。
myarray.sort((a,b) => b.age - a.age)
Is the correct answer but nobody has helped with OP's question of having a hard time understanding sort. 是正确的答案,但没有人帮助OP的问题是很难理解排序。 The function you pass into sort is the comparison function that when comparing two elements of the array should return less than 0 for a comes first, 0 if they are equal and greater than 0 for b comes first.
传递给sort的函数是比较函数,当比较数组的两个元素时,对于第一个应该返回小于0,如果它们相等则返回0,对于b首先返回大于0。
I use this default comparer function in my projects 我在我的项目中使用这个默认比较器函数
defaultCompare = (a, b) => (!a && !b ? 0 : !a ? -1 : !b ? 1 : a < b ? -1 : a > b ? 1 : 0);
as undefined, null, NaN and other falsey values can throw a spanner in there on you. as undefined,null,NaN和其他falsey值可以在你身上抛出一个扳手。
ES6 ES6
myarray.sort((a,b) => b.age - a.age)
ES5 ES5
myarray.sort(function(a,b){return b.age - a.age})
Detailed description of the sort function can be found here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort 有关sort函数的详细说明,请访问https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
*Edited to sort in descending order as OP asked *编辑按OP要求按降序排序
Thanks everyone. 感谢大家。 I solved it by using this option:
我使用这个选项解决了它:
data.sort(function(a, b) { return b.score - a.score })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.