Skip to content

Automatic datetime sorting function crashes when null values exist #4041

Closed

Description

@pavlos256

Describe the bug

A column with mostly Date values will automatically use the datetime sort function, but that function does not handle nulls and crashes on the call to getTime().

Your minimal, reproducible example

https://codesandbox.io/s/hardcore-brook-91xtu9?file=/src/main.tsx

Steps to reproduce

  1. Open sandbox
  2. Click on the createdAt column to sort it
  3. App crashes

Expected behavior

The datetime sorting function should not crash with null / undefined values.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

All

react-table version

8.0.0-beta.9

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Activity

  1. abdullahtariq1171 commented on Jun 24, 2022

    @abdullahtariq1171
    Contributor

    Should we check for nullish values in sortinFns? @tannerlinsley

    This is where it is crashing

    (rowA.getValue(columnId) as Date).getTime(),
    (rowB.getValue(columnId) as Date).getTime()


    @pavlos256 A workaround would be to add your own sortingFn and use that, like below:

    Define your custom sortingFn

    let table = createTable()
    .setOptions({
      sortingFns: {
        sortNullableDate: (rowA: any, rowB: any, columnId: any): number => {
          function compareBasic(a: any, b: any) {
            return a === b ? 0 : a > b ? 1 : -1
          }
    
          return compareBasic(
            (rowA.getValue(columnId) as Date)?.getTime(), // added "?." on this line
            (rowB.getValue(columnId) as Date)?.getTime() // added "?." on this line
          )
        },
      },
    })
    

    use it for desired column

    table.createDataColumn('createdAt', {
      sortingFn: 'sortNullableDate',
    }),
    
    
  2. tannerlinsley commented on Jul 7, 2022

    @tannerlinsley
    Member

    I think we should handle it in the core. PRs are welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions