当前位置:网站首页>【对象数组的排序】
【对象数组的排序】
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));
}
边栏推荐
- 图神经网络+对比学习,下一步去哪?
- notepad++
- LeetCode 496. Next larger element I
- Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
- OpenGL - Lighting
- Kotlin introductory notes (II) a brief introduction to kotlin functions
- Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
- 一次 Keepalived 高可用的事故,让我重学了一遍它
- MySQL does not take effect in sorting string types
- 测试老鸟浅谈unittest和pytest的区别
猜你喜欢
Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
Information and entropy, all you want to know is here
Huber Loss
【ManageEngine】如何利用好OpManager的报表功能
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
OpenGL - Coordinate Systems
Priority queue (heap)
Applet (subcontracting)
Blogger article navigation (classified, real-time update, permanent top)
生成对抗网络
随机推荐
Wxml template syntax
Applet data attribute method
LeetCode 31. 下一个排列
2310. The number of bits is the sum of integers of K
C # image difference comparison: image subtraction (pointer method, high speed)
Global configuration tabbar
混淆矩阵(Confusion Matrix)
编辑器-vi、vim的使用
项目实战 | Excel导出功能
22-07-04 西安 尚好房-项目经验总结(01)
植物大战僵尸Scratch
云计算技术热点
迁移学习和域自适应
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Rebuild my 3D world [open source] [serialization-1]
Kotlin introductory notes (I) kotlin variables and non variables
Deep understanding of C language pointer
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
It's too difficult to use. Long articles plus pictures and texts will only be written in short articles in the future