You could subtract the the code properties inside the compareFunction.(您可以减去compareFunction中的代码属性。)
If both a
and b
have the same code
property, then the ||
(如果a
和b
都具有相同的code
属性,则||
) operator will subtract the subCode
property.(运算符将减去subCode
属性。) The -
operator will coerce the strings to numbers and it will return a numeric value.(-
运算符会将字符串强制转换为数字,并将返回数字值。)
const input=[{id:1,name:"ABP",code:1460,subCode:"0010"},{id:2,name:"GKY",code:1460,subCode:"0030"},{id:3,name:"CPT",code:1410,subCode:"0070"},{id:4,name:"KLB",code:1470,subCode:"0050"},{id:5,name:"POL",code:1430,subCode:"0050"},{id:6,name:"FVB",code:1410,subCode:"0050"},]; input.sort((a, b) => a.code - b.code || a.subCode - b.subCode) console.log(input)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…