diff --git a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx index debef10ae5..8fc502794e 100644 --- a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx +++ b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx @@ -194,9 +194,33 @@ describe('Sort By Func Tests', () => { expect(result).toEqual(originValues); }); - test('should return sortFunc result', () => { + test('should return merged result', () => { + const originValues = ['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州']; + + const result = sortByFunc({ + originValues, + sortParam: { + sortFieldId: 'city', + sortFunc: () => ['浙江[&]杭州'], + }, + dataSet: { + fields: { + rows: ['province', 'city'], + }, + } as unknown as PivotDataSet, + }); + + // sortFunc 返回的值在前,未返回的值在后 + expect(result).toEqual(['浙江[&]杭州', '四川[&]成都', '四川[&]绵阳']); + }); + + test('should return merged result when sorting by ASC', () => { + const originValues = ['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州']; + const result = sortByFunc({ + originValues, sortParam: { + sortMethod: 'ASC', sortFieldId: 'city', sortFunc: () => ['浙江[&]杭州'], }, @@ -207,7 +231,9 @@ describe('Sort By Func Tests', () => { } as unknown as PivotDataSet, }); - expect(result).toEqual(['浙江[&]杭州']); + // asc 升序时 + // sortFunc 没返回的值在前,返回的值在后 + expect(result).toEqual(['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州']); }); test('should return fallback result', () => { diff --git a/packages/s2-core/src/utils/sort-action.ts b/packages/s2-core/src/utils/sort-action.ts index a4507bd060..f93fe3f18b 100644 --- a/packages/s2-core/src/utils/sort-action.ts +++ b/packages/s2-core/src/utils/sort-action.ts @@ -119,7 +119,7 @@ export const sortByCustom = (params: SortActionParams): string[] => { export const sortByFunc = (params: SortActionParams): string[] => { const { originValues, measureValues, sortParam, dataSet } = params; - const { sortFunc, sortFieldId } = sortParam; + const { sortFunc, sortFieldId, sortMethod } = sortParam; const sortResult = sortFunc({ data: measureValues, @@ -145,7 +145,9 @@ export const sortByFunc = (params: SortActionParams): string[] => { originValues, }); } - return sortResult; + + // 用户返回的 sortResult 可能是不全的,需要用原始数据补全 + return mergeDataWhenASC(sortResult, originValues, isAscSort(sortMethod)); }; export const sortByMethod = (params: SortActionParams): string[] => {