排序方法
mounted ( ) {
const oldArr = [ {
'name' : 'a123' ,
'code' : 'a' ,
'snapshotId' : 'ff8081818bd09640018bd09640860000' ,
'showList' : null ,
'orderList' : null ,
'sortOrder' : null ,
'sortNumber' : 2 ,
'isSelected' : false
} , {
'name' : '端口流量' ,
'code' : 'throughput' ,
'snapshotId' : '37' ,
'showList' : null ,
'orderList' : null ,
'sortOrder' : 'asc' ,
'sortNumber' : 0 ,
'isSelected' : true
} , {
'name' : '开启端口数' ,
'code' : 'memutil' ,
'snapshotId' : '38' ,
'showList' : null ,
'orderList' : null ,
'sortOrder' : null ,
'sortNumber' : 1 ,
'isSelected' : false
} ]
const arr = JSON . parse ( JSON . stringify ( oldArr) )
console. log ( '排序之前->' , arr)
const arr2 = oldArr. sort ( this . sortFunc ( 'sortNumber' ) )
console. log ( '排序之后->' , arr2)
}
sortFunc ( name ) {
return function ( o, p ) {
var a, b
if ( typeof o === 'object' && typeof p === 'object' && o && p) {
a = o[ name]
b = p[ name]
if ( a === b) {
return 0
}
if ( typeof a === typeof b) {
return a < b ? - 1 : 1
}
return typeof a < typeof b ? - 1 : 1
}
}
} ,