v-data-table sort strings #21019
-
Hi, When sorting the column with these values ['01', '015', '0158', '02', '0258', '03'], I am getting the following order: ['01', '02', '03', '015', '0158', '0258']. It seems that Vuetify is sorting these values as numbers instead of as strings. How can I avoid this issue and sort them as strings? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To sort the values as strings instead of numbers in Vuetify, you can use the
const headers = [
{
title: 'Values',
key: 'value',
customSort: (a, b) => a.localeCompare(b),
},
// other headers...
];
<template>
<v-data-table
:headers="headers"
:items="items"
:sort-by="[{ key: 'value', order: 'asc' }]"
></v-data-table>
</template>
<script>
export default {
data() {
return {
headers: [
{
title: 'Values',
key: 'value',
customSort: (a, b) => a.localeCompare(b),
},
// other headers...
],
items: [
{ value: '01' },
{ value: '015' },
{ value: '0158' },
{ value: '02' },
{ value: '0258' },
{ value: '03' },
],
};
},
};
</script> This way, the |
Beta Was this translation helpful? Give feedback.
To sort the values as strings instead of numbers in Vuetify, you can use the
customSort
function in your data table configuration. Here is an example of how you can implement it:customSort
function for the specific column:v-data-table
component: