Skip to content

[Table] Zero 0 is not sorted correctly #10556

@CaselIT

Description

@CaselIT

Bug

When sorting over a column of numbers the number zero is sorted incorreclty
Example:
image

What is the expected behavior?

0 should be larger than negative numbers

What is the current behavior?

0 is smaller than negative numbers

Is there anything else we should know?

The problem is probably here
https://github.com/angular/material2/blob/c05005a688a1ff929af966835ae33ca85c106fc9/src/lib/table/table-data-source.ts#L128-L139

0 evaluates to false, while it shouldn't in this case.
I think something along the lines of

if ((valueA || valueA === 0) && (valueB || valueB === 0)) {
  // Check if one value is greater than the other; if equal, comparatorResult should remain 0.
  if (valueA > valueB) {
    comparatorResult = 1;
  } else if (valueA < valueB) {
    comparatorResult = -1;
  }
} else if (valueA || valueA === 0) {
  comparatorResult = 1;
} else if (valueB || valueB === 0) {
  comparatorResult = -1;
}

should fix this issue

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions