当前位置:网站首页>【对象数组的排序】
【对象数组的排序】
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));
}
边栏推荐
- Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
- STM32简易多级菜单(数组查表法)
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
- Rebuild my 3D world [open source] [serialization-2]
- 云计算技术热点
- C # image difference comparison: image subtraction (pointer method, high speed)
- Kotlin introductory notes (V) classes and objects, inheritance, constructors
- Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
- 测试老鸟浅谈unittest和pytest的区别
猜你喜欢
![[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details

LeetCode 496. 下一个更大元素 I

Android privacy sandbox developer preview 3: privacy, security and personalized experience

干货整理!ERP在制造业的发展趋势如何,看这一篇就够了

Applet global style configuration window

Applet data attribute method

Applet network data request

A detailed explanation of the general process and the latest research trends of map comparative learning (gnn+cl)
![[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization

Kotlin introductory notes (VIII) collection and traversal
随机推荐
Unity skframework framework (24), avatar controller third person control
22-07-04 Xi'an Shanghao housing project experience summary (01)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
c语言指针深入理解
Priority queue (heap)
Composition of applet code
mysql安装配置以及创建数据库和表
Confusion matrix
项目实战 | Excel导出功能
VS Code问题:长行的长度可通过 “editor.maxTokenizationLineLength“ 进行配置
Uni app implements global variables
The research trend of map based comparative learning (gnn+cl) in the top paper
LeetCode 31. 下一个排列
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Newton iterative method (solving nonlinear equations)
OpenGL - Coordinate Systems
MySQL does not take effect in sorting string types
云计算技术热点
Node collaboration and publishing
nodejs_ fs. writeFile