按照中文首字母排序
在項目中的表格中要對國家排序,使用的是element中的表格
<el-table-column label="Country" width="100" show-overflow-tooltip sortable :sort-method="sortChange1">
<template slot-scope="scope">
{{ scope.row .company_country_name_en?scope.row.company_country_name_en:scope.row.company_country_name_cn | textFormat }}
</template>
</el-table-column>
- show-overflow-tooltip:當內容過長被隱藏時顯示 tooltip
- sortable:對應列是否可以排序
- sort-method:對數據進行排序的時候使用的方法
//國家首字母排序
sortChange1(a,b){
return a.company_country_name_en.localeCompare(b.company_country_name_en);
},
使用sort()的話無法直接的比較中文,並且在對字符串的比較中 比較的是首位來比較的 例如:
let arr=['10000','1','23','8','25','36']
arr.sort()
console.log(arr)//(6) ["1", "10000", "23", "25", "36", "8"]
所以使用sort就不行了
sort()有一個參數是可設置的排列順序sort會按照順序把arr[i] arr[i+1]傳入方法的sort((a,b)=>...)a和b 比較的規則:
- a小於b,返回小於0的值,sort排序會將a放在b之前
- a等於b,返回0
- a大於b,返回大於0的值,sort排序會將b放在a之前
localeCompare()方法,比較兩個字符串的先后順序,可用於判斷中文 例如:a.localeCompare(b)
- 當前字符串對象排在b之前,則localeCompare()返回 -1
- 當前字符串排在b之后,則返回 1
- 兩個字符串相等,則返回 0
//可使用的方法為:
arr.sort((a,b)=>{
return a.localeCompare(b)
})
