1. 程式人生 > 其它 >Vue-ElementUI:排序——物件集合按照物件屬性排序

Vue-ElementUI:排序——物件集合按照物件屬性排序

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 //要排序的資料 letdata = [ {chinese:'蔡司', english:'Chase'}, {chinese:'艾倫', english:'Allen'}, {chinese:'左拉', english:'Zola'}, {chinese:'貝克', english:'Baker'}, {chinese:'伯格', english:'Berg'}, {chinese:'菲奇', english:'Fitch'}, {chinese:'迪安', english:'Dean'}, {chinese:
'厄爾', english:'Earle'}, {chinese:'亨利', english:'Henry'}, ] 中文:data.sort((a, b)=> b.chinese.localeCompare(a.chinese,'zh'));//z~a 排序 中文: data.sort((a, b)=> a.chinese.localeCompare(b.chinese,'zh'));//a~z 排序 英文:data.sort((a, b) => b.english.charCodeAt(0) - a.english.charCodeAt(0));//z~a 排序 英文:data.sort((a, b) => a.english.charCodeAt(0) - b.english.charCodeAt(0));
//a~z 排序

本篇整理參考引用::https://www.cnblogs.com/wangrui38/p/12467589.html

例項:

描述:對當前 table 的資料 this.tableInfo.data 按照dealerName和delivererName降序排序

this.tableInfo.data.sort((a,b)=>{ constsortA=a.dealerName+a.delivererName constsortB=b.dealerName+b.delivererName returnsortB.localeCompare(sortA,'zh') })