Skip to content

Sort numeric values in sortable columns numerically - #1083

Open
NopeCharon wants to merge 1 commit into
pytest-dev:masterfrom
NopeCharon:numeric-column-sort
Open

NopeCharon wants to merge 1 commit into
pytest-dev:masterfrom
NopeCharon:numeric-column-sort

Conversation

@NopeCharon

Copy link
Copy Markdown

Sortable columns other than Result and Duration were always compared as strings. Custom columns holding numbers (e.g. a speedup column added through pytest_html_results_table_header/pytest_html_results_table_row) were therefore sorted lexicographically: 10.5 came before 9.25.

Changes

  • genericSort now uses a compareValues helper. When both values are numbers it compares them numerically; otherwise it keeps the existing string comparison.
  • If a column mixes numeric and non-numeric values (e.g. n/a), numbers go before the rest. This keeps the comparator a consistent total order instead of mixing numeric and string comparisons.
  • Empty or whitespace-only cells are not treated as 0.
  • The Result (custom order), Duration and test id sorting behave exactly as before.
  • Added a short note about numeric sorting to the "Results Table Sorting" section of the user guide.

Testing

  • JS unit test sort on numeric column in testing/unittest.js.
  • E2E test test_custom_numeric_sorting in testing/test_e2e.py: sorts a custom numeric column through the sort query parameter and then by clicking the header.

Both tests fail on master and pass with this change. npm run lint and npm run unit pass.

Fixes #818

Sortable columns other than result and duration were always compared
as strings, so custom columns holding numbers (e.g. a "speedup" column
added through pytest_html_results_table_header/row) were ordered
lexicographically: "10.5" came before "9.25".

Compare two values numerically when both are numbers, and keep the
existing string comparison otherwise. When a column mixes numeric and
non-numeric values, numbers are placed before the rest so the
comparison stays a consistent total order.

Add a JavaScript unit test and an end-to-end test for sorting a
custom numeric column, and mention numeric sorting in the user guide.

Fixes pytest-dev#818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Got incorrect sort when the content of cell is the numeric value of the float type.

1 participant