当前位置:网站首页>[sorting of object array]
[sorting of object array]
2022-07-05 09:28:00 【Guodong pudding】
/**
* @desc Sort the array elements according to the specified attributes in the object array
* @param objArr An array of objects to sort
* @param attribute Sort properties
* @param orderFlag Mode identification ,true In the positive order false For reverse order The default is true
* @returns Array after sorting
*/
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));
}
边栏推荐
- 【对象数组的排序】
- OpenGL - Coordinate Systems
- Unity skframework framework (XXIII), minimap small map tool
- 【两个对象合并成一个对象】
- Nodejs modularization
- Svg optimization by svgo
- Kotlin introductory notes (II) a brief introduction to kotlin functions
- Kotlin introductory notes (III) kotlin program logic control (if, when)
- Priority queue (heap)
- 初识结构体
猜你喜欢
随机推荐
【js 根据对象数组中的属性进行排序】
Applet data attribute method
np. allclose
mysql安装配置以及创建数据库和表
Unity skframework framework (24), avatar controller third person control
AUTOSAR from getting started to mastering 100 lectures (103) -dbc file format and creation details
[beauty of algebra] solution method of linear equations ax=0
nodejs_ 01_ fs. readFile
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
uni-app---uni.navigateTo跳转传参使用
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Kotlin introductory notes (VII) data class and singleton class
2310. 个位数字为 K 的整数之和
The research trend of map based comparative learning (gnn+cl) in the top paper
信息与熵,你想知道的都在这里了
Applet (global data sharing)
【对象数组的排序】
Generate confrontation network
Information and entropy, all you want to know is here