sortingdategwtcelltable

How to sort Date Column with empty cells in CellTable GWT


I have a CellTable in GWT and a Date Column. But the cell of this column may be empty

enter image description here

I want to add sorting by order to this column like this.

enter image description hereenter image description here

and if the column is empty at all, I don't need to sort table.

I wrote the comparator

public class RegDateComparator implements Comparator<DocList> {

    @Override
    public int compare(DocList o1, DocList o2) {
        if (o1 == o2) {
            return 0;
        }
        Date date1 = new Date();
        Date date2 = new Date();
        if (o1 != null) {
            DateTimeFormat dateFormat = DateTimeFormat
                    .getFormat("dd.MM.yyyy");
            if (!o1.getRegistrationDate().isEmpty()) {
                date1 = dateFormat.parse(o1.getRegistrationDate());
            } else {
                return -1;
            }
            if (!o2.getRegistrationDate().isEmpty()) {
                date2 = dateFormat.parse(o2.getRegistrationDate());
            } else {
                return -1;
            }

            return (o2 != null) ? date1.compareTo(date2) : 1;
        }
        return -1;
    }
}

But my code doesn't sorting well. And if the column is empty, the sorting is still working. Help me) Thanks


Solution

  • I resolve my issue

    public class RegDateComparator implements Comparator<DocList> {
    
        @Override
        public int compare(DocList o1, DocList o2) {
            if (o1 == o2) {
                return 0;
            }
            DateTimeFormat dateFormat = DateTimeFormat
                    .getFormat("dd.MM.yyyy");
            // Compare the filing date columns.
            if (o1 != null) {
                Date date1 = dateFormat.parse("01.01.1900");
                Date date2 = dateFormat.parse("01.01.1900");
                if (!o1.getRegistrationDate().isEmpty()) {
                    date1 = dateFormat.parse(o1.getRegistrationDate());
                }
                if (!o2.getRegistrationDate().isEmpty()) {
                    date2 = dateFormat.parse(o2.getRegistrationDate());
                }
                return (o2 != null) ? date1.compareTo(date2) : 1;
            }
            return -1;
        }
    }
    

    When I had only Date date1 = new Date(), date1 contained the current date, so it was always greater than the possible dates and null dates were always in the end, and I wanted them to be in the beginning. So I parse it to "01.01.1900". If you want, you can give me advices to improve my code. Thanks!)