Fixes https://github.com/twentyhq/twenty/issues/5762 The problem was only happening with a sort on a select type field, also appears with currency type and maybe other types. This was because NULL values were sorted in a random order because the sort function was comparing two NULL values as not equal, so I just added a case for when A === B === NULL.
29 lines
731 B
TypeScript
29 lines
731 B
TypeScript
import { Maybe } from '~/generated/graphql';
|
|
|
|
export const sortNullsFirst = (
|
|
fieldValueA: Maybe<unknown>,
|
|
fieldValueB: Maybe<unknown>,
|
|
) =>
|
|
fieldValueA === null && fieldValueB === null
|
|
? 0
|
|
: fieldValueA === null
|
|
? -1
|
|
: fieldValueB === null
|
|
? 1
|
|
: 0;
|
|
|
|
export const sortNullsLast = (
|
|
fieldValueA: Maybe<unknown>,
|
|
fieldValueB: Maybe<unknown>,
|
|
) => sortNullsFirst(fieldValueB, fieldValueA);
|
|
|
|
export const sortAsc = (
|
|
fieldValueA: string | number,
|
|
fieldValueB: string | number,
|
|
) => (fieldValueA === fieldValueB ? 0 : fieldValueA < fieldValueB ? -1 : 1);
|
|
|
|
export const sortDesc = (
|
|
fieldValueA: string | number,
|
|
fieldValueB: string | number,
|
|
) => sortAsc(fieldValueB, fieldValueA);
|