Skip to content

Commit

Permalink
fix: 修复存在 0 时,数值为 number 时,排序错误的问题 (#1644)
Browse files Browse the repository at this point in the history
* fix: 修复存在 0 时,排序错误的问题

* test: 补充存在 0 时,排序错误的单测

* fix: 修复单测字典序排序错误

Co-authored-by: zishang <lyl275911@antgroup.com>
  • Loading branch information
stone-lyl and zishang authored Aug 4, 2022
1 parent 9d5d8b7 commit 8138c69
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
27 changes: 27 additions & 0 deletions packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ describe('Sort Action Test', () => {
expect(sortAction(data2, 'DESC')).toEqual(['3', '2', '11']);
});

test('sort action with zero and number arr', () => {
const data1 = [1, 6, -2, 0];
expect(sortAction(data1, 'ASC')).toEqual([-2, 0, 1, 6]);
expect(sortAction(data1, 'DESC')).toEqual([6, 1, 0, -2]);

const data2 = ['0', 0, 2, -2];
expect(sortAction(data2, 'ASC')).toEqual([-2, '0', 0, 2]);
expect(sortAction(data2, 'DESC')).toEqual([2, '0', 0, -2]);
});

test('sort action with string arr', () => {
const data = ['a', 'c', 'b'];
expect(sortAction(data, 'ASC')).toEqual(['a', 'b', 'c']);
Expand All @@ -49,6 +59,22 @@ describe('Sort Action Test', () => {
expect(sortAction(data2, 'DESC')).toEqual(['啊', '2', '11']);
});

test('object data sorted by key with zero', () => {
const data1 = [{ a: 1 }, { a: 0 }, { a: -3 }, { a: 2 }];
expect(sortAction(data1, 'ASC', 'a')).toEqual([
{ a: -3 },
{ a: 0 },
{ a: 1 },
{ a: 2 },
]);
expect(sortAction(data1, 'DESC', 'a')).toEqual([
{ a: 2 },
{ a: 1 },
{ a: 0 },
{ a: -3 },
]);
});

test('sort action with object arr', () => {
const data1 = [{ a: 1 }, { a: 3 }, { a: 2 }];
expect(sortAction(data1, 'ASC', 'a')).toEqual([
Expand Down Expand Up @@ -93,6 +119,7 @@ describe('Sort Action Test', () => {
'a',
),
).toEqual([{ a: undefined }, { a: '-' }, { a: 2 }, { a: '3' }]);

expect(
sortAction(
[{ a: '-' }, { a: '3' }, { a: 2 }, { a: undefined }],
Expand Down
17 changes: 11 additions & 6 deletions packages/s2-core/src/utils/sort-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
includes,
indexOf,
isEmpty,
isNaN,
isNil,
keys,
map,
split,
Expand All @@ -22,6 +24,8 @@ export const isAscSort = (sortMethod) => toUpper(sortMethod) === 'ASC';

export const isDescSort = (sortMethod) => toUpper(sortMethod) === 'DESC';

const canTobeNumber = (a?: string | number) => !isNaN(Number(a));

/**
* 执行排序
* @param list - 待排序数组
Expand All @@ -37,12 +41,12 @@ export const sortAction = (
const specialValues = ['-', undefined];
return list?.sort(
(pre: string | number | DataType, next: string | number | DataType) => {
let a = pre;
let b = next;
let a = pre as string | number;
let b = next as string | number;
if (key) {
a = pre[key];
b = next[key];
if (Number(a) && Number(b)) {
a = pre[key] as string | number;
b = next[key] as string | number;
if (canTobeNumber(a) && canTobeNumber(b)) {
return (Number(a) - Number(b)) * sort;
}
if (a && specialValues?.includes(a?.toString())) {
Expand All @@ -52,7 +56,8 @@ export const sortAction = (
return sort;
}
}
if (a && b) {
// 没有参数 key 时,需要理解成按字典序(首字母)进行排序,用于排维度值的。(我也不理解为啥要把这两个逻辑写在一起,很容易误解
if (!isNil(a) && !isNil(b)) {
// 数据健全兼容,用户数据不全时,能够展示.
return a.toString().localeCompare(b.toString(), 'zh') * sort;
}
Expand Down

1 comment on commit 8138c69

@vercel
Copy link

@vercel vercel bot commented on 8138c69 Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

antvis-s2 – ./s2-site

antvis-s2.vercel.app
antvis-s2-antv-s2.vercel.app
antvis-s2-git-master-antv-s2.vercel.app

Please sign in to comment.