You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2022/06/23 11:39:54 UTC
[superset] branch master updated: fix: issue with sorting by multiple columns in a table (#19920)
This is an automated email from the ASF dual-hosted git repository.
michaelsmolina pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new a45d011e74 fix: issue with sorting by multiple columns in a table (#19920)
a45d011e74 is described below
commit a45d011e74be7a52fee9b0e580187dd6f25509db
Author: stevetracvc <70...@users.noreply.github.com>
AuthorDate: Thu Jun 23 05:39:46 2022 -0600
fix: issue with sorting by multiple columns in a table (#19920)
Recent commit to sort alphanumeric columns via case insensitive
comparison broke the multi-column sort option. React-table only sorts
by the second (or third...) column if the first column matches.
Since the alphanumeric sort only returned -1 or 1, it never would move
to the subsequent columns when the earlier column values matched.
---
.../utils/sortAlphanumericCaseInsensitive.ts | 2 +-
.../test/sortAlphanumericCaseInsensitive.test.ts | 108 +++++++++++++++++++++
2 files changed, 109 insertions(+), 1 deletion(-)
diff --git a/superset-frontend/plugins/plugin-chart-table/src/DataTable/utils/sortAlphanumericCaseInsensitive.ts b/superset-frontend/plugins/plugin-chart-table/src/DataTable/utils/sortAlphanumericCaseInsensitive.ts
index 789e0cef7f..c1adc99949 100644
--- a/superset-frontend/plugins/plugin-chart-table/src/DataTable/utils/sortAlphanumericCaseInsensitive.ts
+++ b/superset-frontend/plugins/plugin-chart-table/src/DataTable/utils/sortAlphanumericCaseInsensitive.ts
@@ -33,5 +33,5 @@ export const sortAlphanumericCaseInsensitive = <D extends {}>(
if (!valueB || typeof valueB !== 'string') {
return 1;
}
- return valueA.localeCompare(valueB) > 0 ? 1 : -1;
+ return valueA.localeCompare(valueB);
};
diff --git a/superset-frontend/plugins/plugin-chart-table/test/sortAlphanumericCaseInsensitive.test.ts b/superset-frontend/plugins/plugin-chart-table/test/sortAlphanumericCaseInsensitive.test.ts
index 4ba35ba602..356596ec21 100644
--- a/superset-frontend/plugins/plugin-chart-table/test/sortAlphanumericCaseInsensitive.test.ts
+++ b/superset-frontend/plugins/plugin-chart-table/test/sortAlphanumericCaseInsensitive.test.ts
@@ -17,8 +17,13 @@
* under the License.
*/
+import { defaultOrderByFn, Row } from 'react-table';
import { sortAlphanumericCaseInsensitive } from '../src/DataTable/utils/sortAlphanumericCaseInsensitive';
+type RecursivePartial<T> = {
+ [P in keyof T]?: T[P] | RecursivePartial<T[P]>;
+};
+
const testData = [
{
values: {
@@ -133,3 +138,106 @@ describe('sortAlphanumericCaseInsensitive', () => {
]);
});
});
+
+const testDataMulti: Array<RecursivePartial<Row<object>>> = [
+ {
+ values: {
+ colA: 'group 1',
+ colB: '10',
+ },
+ },
+ {
+ values: {
+ colA: 'group 1',
+ colB: '15',
+ },
+ },
+ {
+ values: {
+ colA: 'group 1',
+ colB: '20',
+ },
+ },
+ {
+ values: {
+ colA: 'group 2',
+ colB: '10',
+ },
+ },
+ {
+ values: {
+ colA: 'group 3',
+ colB: '10',
+ },
+ },
+ {
+ values: {
+ colA: 'group 3',
+ colB: '15',
+ },
+ },
+ {
+ values: {
+ colA: 'group 3',
+ colB: '10',
+ },
+ },
+];
+
+describe('sortAlphanumericCaseInsensitiveMulti', () => {
+ it('Sort rows', () => {
+ const sorted = defaultOrderByFn(
+ [...testDataMulti] as Array<Row<object>>,
+ [
+ (a, b) => sortAlphanumericCaseInsensitive(a, b, 'colA'),
+ (a, b) => sortAlphanumericCaseInsensitive(a, b, 'colB'),
+ ],
+ [true, false],
+ );
+
+ expect(sorted).toEqual([
+ {
+ values: {
+ colA: 'group 1',
+ colB: '20',
+ },
+ },
+ {
+ values: {
+ colA: 'group 1',
+ colB: '15',
+ },
+ },
+ {
+ values: {
+ colA: 'group 1',
+ colB: '10',
+ },
+ },
+ {
+ values: {
+ colA: 'group 2',
+ colB: '10',
+ },
+ },
+ {
+ values: {
+ colA: 'group 3',
+ colB: '15',
+ },
+ },
+ {
+ values: {
+ colA: 'group 3',
+ colB: '10',
+ },
+ },
+ {
+ values: {
+ colA: 'group 3',
+ colB: '10',
+ },
+ },
+ ]);
+ });
+});