当前位置:网站首页>[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;
}
}
边栏推荐
- What is a firewall? Explanation of basic knowledge of firewall
- 顶会论文看图对比学习(GNN+CL)研究趋势
- [beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
- NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
- Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
- nodejs_ fs. writeFile
- 【饿了么动态表格】
- 22-07-04 西安 尚好房-项目经验总结(01)
- Unity SKFramework框架(二十三)、MiniMap 小地图工具
- Analysis of eventbus source code
猜你喜欢

VS Code问题:长行的长度可通过 “editor.maxTokenizationLineLength“ 进行配置

A keepalived high availability accident made me learn it again

LeetCode 503. 下一个更大元素 II

Shutter uses overlay to realize global pop-up

Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning

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

Generate confrontation network

OpenGL - Model Loading
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

LeetCode 556. 下一个更大元素 III
随机推荐
Rebuild my 3D world [open source] [serialization-1]
浅谈Label Smoothing技术
Unity skframework framework (XXII), runtime console runtime debugging tool
Analysis of eventbus source code
OpenGL - Coordinate Systems
【js 根据对象数组中的属性进行排序】
Hi Fun Summer, play SQL planner with starrocks!
Deep understanding of C language pointer
uni-app---uni.navigateTo跳转传参使用
OpenGL - Lighting
阿里云发送短信验证码
Kotlin introductory notes (III) kotlin program logic control (if, when)
利用请求头开发多端应用
Shutter uses overlay to realize global pop-up
Introduction Guide to stereo vision (7): stereo matching
[ManageEngine] how to make good use of the report function of OpManager
云计算技术热点
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
深入浅出PyTorch中的nn.CrossEntropyLoss
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis