Skip to content

[Grid] Column ignores custom reversed comparator implementation #3926

@SomMeri

Description

@SomMeri

Description

I use the grid.setComparator(<>) method to supply comparator to the grid. My comparator has custom reverse comparator - I do NOT want the default one. Vaadin ignores my reverse comparator and uses own reverse comparator.

Here is setComparator method and here is getComparator method.

I need the setComparator to do something like this:

        public Column<T> setComparator(Comparator<T> comparator) {
            Objects.requireNonNull(comparator, "Comparator must not be null");
            setSortable(true);
            this.comparator = comparator::compare;
            this.reverseComparator = comparator.reversed()::compare; // <----- new line here
            return this;
        }

and the getComparator to do:

        public SerializableComparator<T> getComparator(
                SortDirection sortDirection) {
            Objects.requireNonNull(comparator,
                    "No comparator defined for sorted column.");
            setSortable(true);
            boolean reverse = sortDirection != SortDirection.ASCENDING;
            return reverse ? reverseComparator : comparator; <----- change here
        }

Reason: I need to implement "favorites" functionality - a set of rows that is up even if the order is descending. The "natural" way to implement it would be to use reverse comparator for that.

Expected outcome

If I supply a comparator with custom reverse comparator, vaadin should use that one to to compare rows.

Minimal reproducible example

    Grid<Integer> grid2 = new Grid<>();
    Comparator<Integer> comparator = new Comparator<Integer>() {

      @Override
      public int compare(Integer o1, Integer o2) {
        return Comparator
          .comparing(this::isFavorite)
          .thenComparing(getNestedComparator())
          .compare(o1, o2);
      }

      @Override
      public Comparator<Integer> reversed() {
        return Comparator
          .comparing(this::isFavorite)
          .thenComparing(getNestedComparator().reversed());
      }

      private Comparator<Integer> getNestedComparator() {
        return Comparator.naturalOrder();
      }

      private boolean isFavorite(Integer t) {
        return t % 2 == 0;
      }

    };
    Column<Integer> c = grid2.addColumn(i -> String.valueOf(i)).setHeader("Text");
    c.setComparator(comparator);

grid2.setItems(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Steps to reproduce

Try to sort the above grid. Odd numbers should be always on top. It should be:

  • Ascending: 1 3 5 7 9 <<- favorites | less important ->> 2 4 6 8 10
  • Descending: 9 7 5 3 1 <<- favorites | less important ->> 10 8 6 4

It is:

  • Ascending: 1 3 5 7 9 <<- favorites | less important ->> 2 4 6 8 10
  • Descending: 10 8 6 4 2 < | favorites are fown ->> 9 7 5 3 1

Environment

Vaadin version(s):
OS:

Browsers

No response

Environment

Vaadin version(s):
OS:

Browsers

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions