当前位置:网站首页>【js 根据对象数组中的属性进行排序】
【js 根据对象数组中的属性进行排序】
2022-07-05 09:27:00 【果东布丁】
【js 根据对象数组中的属性进行排序】
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')))
* 使用例子:newArray.sort(sortBy('number',false)) //表示根据number属性降序排列;若第二个参数不传递,默认表示升序排序
* @param attr 排序的属性 如number属性
* @param rev true表示升序排列,false降序排序
* */
sortBy: function(attr,rev){
//第二个参数没有传递 默认升序排列
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;
}
}
边栏推荐
- C # compare the differences between the two images
- np. allclose
- LeetCode 31. 下一个排列
- 混淆矩阵(Confusion Matrix)
- Transfer learning and domain adaptation
- OpenGL - Coordinate Systems
- 2310. The number of bits is the sum of integers of K
- Introduction Guide to stereo vision (7): stereo matching
- [team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
- STM32 simple multi-level menu (array table lookup method)
猜你喜欢

编辑器-vi、vim的使用
![[ManageEngine] how to make good use of the report function of OpManager](/img/15/dc15e638ae86d6cf1d5b989fe56611.jpg)
[ManageEngine] how to make good use of the report function of OpManager

mysql安装配置以及创建数据库和表

Lepton 无损压缩原理及性能分析

L'information et l'entropie, tout ce que vous voulez savoir est ici.
![一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]](/img/c4/27ae0d259abc4e61286c1f4d90c06a.png)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]

Shutter uses overlay to realize global pop-up

植物大战僵尸Scratch

Generate confrontation network
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]
随机推荐
顶会论文看图对比学习(GNN+CL)研究趋势
OpenGL - Coordinate Systems
OpenGL - Lighting
Progressive JPEG pictures and related
Kotlin introductory notes (VI) interface and function visibility modifiers
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
The research trend of map based comparative learning (gnn+cl) in the top paper
阿里十年测试带你走进APP测试的世界
浅谈Label Smoothing技术
Wxss template syntax
22-07-04 Xi'an Shanghao housing project experience summary (01)
Figure neural network + comparative learning, where to go next?
迁移学习和域自适应
notepad++
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Unity skframework framework (XXIII), minimap small map tool
初识结构体
Global configuration tabbar
Svg optimization by svgo
Applet (global data sharing)