当前位置:网站首页>【对象数组的排序】
【对象数组的排序】
2022-07-05 09:27:00 【果东布丁】
/**
* @desc 根据对象数组中的指定属性进行数组元素排序
* @param objArr 要进行排序操作的对象数组
* @param attribute 排序属性
* @param orderFlag 方式标识,true 为正序 false 为倒序 默认为 true
* @returns 完成排序后的数组
*/
export function objectArraySortByAtr(objArr, attribute,orderFlag=true) {
function compare(key,sortFlag) {
let sortFlagRes;
if(sortFlag){
sortFlagRes=1;
} else{
sortFlagRes = sortFlag?1:-1;
}
return function(value1, value2) {
const val1 = value1[key];
const val2 = value2[key];
if(val1<val2){
return sortFlagRes*-1;
}
if(val1>val2){
return sortFlagRes*1;
}
return 0;
};
}
return objArr.sort(compare(`${attribute}`,orderFlag));
}
边栏推荐
- Nodemon installation and use
- Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
- Explain NN in pytorch in simple terms CrossEntropyLoss
- Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
- mysql安装配置以及创建数据库和表
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- Understanding rotation matrix R from the perspective of base transformation
- 高性能Spark_transformation性能
- 一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
- Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
猜你喜欢
随机推荐
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
[reading notes] Figure comparative learning gnn+cl
np.allclose
LeetCode 496. Next larger element I
Principle and performance analysis of lepton lossless compression
Nodejs modularization
C form click event did not respond
信息与熵,你想知道的都在这里了
太不好用了,长文章加图文,今后只写小短文
OpenGL - Lighting
生成对抗网络
驾驶证体检医院(114---2 挂对应的医院司机体检)
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
2310. The number of bits is the sum of integers of K
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
阿里云发送短信验证码
Jenkins pipeline method (function) definition and call
LeetCode 496. 下一个更大元素 I
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Editor use of VI and VIM









