Search code examples
datevaadinvaadin-flowcolumnsortingvaadin-grid

Vaadin Grid sortable date column


i want to add a sortable date column to my vaadin grid component. Unfortunately it doesn't work with formatted dates, I think after formatting the date object it is just a String and therefore sorting with this column doesn't work correctly, but I need anyway a solution for that. I already have tried this solution, which also doesn't work:

    grid.addColumn(new LocalDateTimeRenderer<>(MyObject::getCreated,
                DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM)
                        .withLocale(Locale.GERMANY).withZone(ZoneId.of("Europe/Paris"))))
                            .setHeader("Created").setSortProperty("created");

Do you have an another idea?

Thanks


Solution

  • You can set a comparator

    grid.addColumn(new LocalDateTimeRenderer<>(MyObject::getCreated,
                    DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM)
                               .withLocale(Locale.GERMANY).withZone(ZoneId.of("Europe/Paris"))))
         .setHeader("Created")
         .setSortProperty("created")
         .setComparator(MyObject::getCreated);
    

    Please also check the documentation: https://vaadin.com/docs/latest/ds/components/grid#sorting