提交 cc1edb0b 编写于 作者: 杨广俊's avatar 杨广俊

排序优化

上级 34b3acfd
......@@ -56,6 +56,54 @@ export function deepCopy(obj) {
return result;
}
/**
*
* @param {Array} originData 初始数据
* @param {Boolean} isUp true: 升序 false: 降序
* @param {String} sortField 排序字段,如果没有,则直接排序
* @param {Function} converFun 字段转换函数
*/
export function arraySort(originData, isUp = true, sortedField, converFun) {
let sortedData = originData.slice(), swap, sourceA, sourceB;
for(let i = 0; i < sortedData.length - 1; i ++) {
for(let j = 0; j < sortedData.length - i -1; j++) {
sourceA = sortedData[j]
sourceB = sortedData[j + 1]
if(sortedField && !converFun) {
sourceA = sourceA[sortedField]
sourceB = sourceB[sortedField]
} else if(sortedField && converFun && typeof converFun === 'function'){
if(!sourceA[sortedField] || !sourceB[sortedField]) {
sourceA = 0
sourceB = 0
} else {
sourceA = converFun(sourceA[sortedField])
sourceB = converFun(sourceB[sortedField])
}
}
if(!sourceA || !sourceB) {
sourceA = 0
sourceB = 0
}
if(isUp) {
if(sourceA - 0 > sourceB - 0) {
swap = sortedData[j]
sortedData[j] = sortedData[j + 1]
sortedData[j + 1] = swap
}
} else {
if(sourceA - 0 < sourceB - 0) {
swap = sortedData[j]
sortedData[j] = sortedData[j + 1]
sortedData[j + 1] = swap
}
}
}
}
console.log(sortedData)
return sortedData
}
export function pageJumpUrl(){
let url = {
pageListUrl : 'https://test1-contents.yunqueyi.com/content_list',
......
此差异已折叠。
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册