Search code examples
vuetify.jsvuetifyjs3

move Null Values to the end


how can i move Null Values to the end Regardless of the Sorting Direction using v-data-table and vuetify3.

Above i have attached a reproducable example, i want to the null values apears at the end of the sorting:

Thanks is advance!

Vuetify Playground


Solution

  • What you need to return is undefined to put null/undefined values at the end of the sort regardless of direction.

    customKeySort: {
      value: (d1, d2) => {
        // Check for null/undefined values and sort them to the end
        if (!d1 && !d2) return undefined
        if (!d1) return undefined
        if (!d2) return undefined
        if (d1 > d2) return 1
        if (d2 > d1) return -1
    
        return 0
      },
    },
    

    updated Vuetify playground