当前位置:网站首页>[JS sort according to the attributes in the object array]
[JS sort according to the attributes in the object array]
2022-07-05 09:28:00 【Guodong pudding】
【js Sort according to the attributes in the object array 】
var arr = [
{name:'a',age:9},
{name:'b',age:12},
{name:'c',age:8}
];
arr.sort((a,b)=>{
return a.age - b.age
});
function compare(property){
return function(a,b){
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
console.log(arr.sort(compare('age')))
* Examples of use :newArray.sort(sortBy('number',false)) // Express basis number Attribute descending ; If the second parameter is not passed , Default means ascending sort
* @param attr The properties of sorting Such as number attribute
* @param rev true Indicates ascending order ,false null
* */
sortBy: function(attr,rev){
// The second parameter is not passed Default ascending order
if(rev == undefined){
rev = 1;
}else{
rev = (rev) ? 1 : -1;
}
return function(a,b){
a = a[attr];
b = b[attr];
if(a < b){
return rev * -1;
}
if(a > b){
return rev * 1;
}
return 0;
}
}
边栏推荐
- Priority queue (heap)
- A keepalived high availability accident made me learn it again
- . Net service governance flow limiting middleware -fireflysoft RateLimit
- 微信小程序获取住户地区信息
- Wechat applet obtains household area information
- C # compare the differences between the two images
- Applet global style configuration window
- 【两个对象合并成一个对象】
- Rebuild my 3D world [open source] [serialization-1]
- LeetCode 31. 下一个排列
猜你喜欢

C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边

一篇文章带你走进cookie,session,Token的世界

Svgo v3.9.0+
![Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]](/img/c4/27ae0d259abc4e61286c1f4d90c06a.png)
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]

【ManageEngine】如何利用好OpManager的报表功能

Kotlin introductory notes (VII) data class and singleton class

一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势

c语言指针深入理解

LeetCode 556. 下一个更大元素 III

Figure neural network + comparative learning, where to go next?
随机推荐
2311. 小于等于 K 的最长二进制子序列
An article takes you into the world of cookies, sessions, and tokens
Project practice | excel export function
VS Code问题:长行的长度可通过 “editor.maxTokenizationLineLength“ 进行配置
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
22-07-04 Xi'an Shanghao housing project experience summary (01)
Talking about label smoothing technology
OpenGL - Lighting
Principle and performance analysis of lepton lossless compression
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
【数组的中的某个属性的监听】
Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
【对象数组的排序】
Confusion matrix
The research trend of map based comparative learning (gnn+cl) in the top paper
Global configuration tabbar
【js 根据对象数组中的属性进行排序】
Kotlin introductory notes (V) classes and objects, inheritance, constructors
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details