columnDefs: [
{
field: 'a', // A 列
rowGroup: true, // 分组
sort: 'asc', // 默认升序排序
sortIndex: 0, // 排序优先级 1
comparator: (valueA, valueB) => customAlphabetComparator(valueA, valueB), // 使用自定义排序逻辑
},
{
field: 'b', // B 列
sort: 'asc',
sortIndex: 1,
comparator: (valueA, valueB) => customAlphabetComparator(valueA, valueB), // 同样应用到其他列
},
{
field: 'c', // C 列
sort: 'asc',
sortIndex: 2,
comparator: (valueA, valueB) => customAlphabetComparator(valueA, valueB), // 同样应用到其他列
},
],
defaultColDef: {
sortable: true, // 确保列可排序
},
groupDefaultExpanded: -1, // 默认展开所有分组
animateRows: true,
};
// 自定义比较函数
function customAlphabetComparator(valueA, valueB) {
const strA = valueA || ''; // 防止 null 或 undefined
const strB = valueB || '';
const len = Math.min(strA.length, strB.length);
for (let i = 0; i < len; i++) {
const charA = strA.charCodeAt(i); // 获取 A 的字符编码
const charB = strB.charCodeAt(i); // 获取 B 的字符编码
if (charA !== charB) {
return charA - charB; // 返回字符编码的差值,正值为 A > B,负值为 A < B
}
}
// 如果前几位相等,则长度短的字符串排在前面
return strA.length - strB.length;
}