本人使用版本是1.4.7
在這個版本中對應全是String的column進行排序並不是按照拼音的方式排列的。
這里我貼一下源代碼就可以看出是為什么了:
export const orderBy = function(array, sortKey, reverse, sortMethod) { if (typeof reverse === 'string') { reverse = reverse === 'descending' ? -1 : 1; } if (!sortKey && !sortMethod) { return array; } const order = (reverse && reverse < 0) ? -1 : 1; // sort on a copy to avoid mutating original array return array.slice().sort(sortMethod ? function(a, b) { return sortMethod(a, b) ? order : -order; } : function(a, b) { if (sortKey !== '$key') { if (isObject(a) && '$value' in a) a = a.$value; if (isObject(b) && '$value' in b) b = b.$value; } a = isObject(a) ? getValueByPath(a, sortKey) : a; b = isObject(b) ? getValueByPath(b, sortKey) : b; return a === b ? 0 : a > b ? order : -order; }); };
關鍵就在:
return a === b ? 0 : a > b ? order : -order; return sortMethod(a, b) ? order : -order;
本人之前直接用
a.brandName.localeCompare(b.brandName)
導致排序出問題,因為源代碼中只接受true與false,而且localeCompare會返回1、0、-1,-1會被轉化為true,從而造成了bug。正確方式:
return a.brandName.localeCompare(b.brandName)>0 ? 1:0;
將-1轉化為0,這樣就正確了
ps.需要判斷a與b是否為空,如果為null就賦值為‘’
2017/12/14升級了2.08發現源代碼的判斷方式改掉了,所以需要改成:
return a.brandName.localeCompare(b.brandName)>0;