question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Column custom sort: pass row data to comparator

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here, post on Stackoverflow or Gitter

Current behavior When TableColumn.comparator is used, arguments passed to the comparator are values of that column.

Expected behavior It would be nice to have an option to receive row data to comparator.

Due to backward compatibility it would be nice to add another Input to TableColumn, e.g. rowComparator(row, row), not to break legacy code.

What is the motivation / use case for changing the behavior?

My specific case is I am displaying time (duration) in a column (e.g. “1d 20h 14m”, “5h 20m”). In other column there is the same value, as number in seconds. Obviously, alphanumeric sorting is wrong in this case and I need to sort the column by seconds.

Since comparator feeds me with string value I must parse the duration back to seconds despite the fact that the value is already present in the table model.

I believe this would be handy in other cases when sortable value is already present however the displayed value is somehow “humanized”.

  • Table version: 9.3.0

  • Angular version:

4.1.3 but that is not relevant I believe

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9

github_iconTop GitHub Comments

8reactions
Ryan-Hainescommented, Apr 25, 2018

For anyone else confused as to how to use a custom comparator in a template, you can pass the row data to the comparator by putting prop="" in the template, like this:

<ngx-datatable-column name="Reviewed" prop="" [comparator]="sortReviewedComparator">
	<ng-template let-row="row" ngx-datatable-cell-template>
		<span {{(row.numReviewed/row.numEntries)*100||0}}% Reviewed">{{row.numReviewed}}/{{row.numEntries}}</span>
	</ng-template>
</ngx-datatable-column>
2reactions
Ecksterscommented, Nov 3, 2017

Latest version has this feature, both rows are passed as the 3rd and 4th parameters to the comparator function, although I don’t think it’s been pushed to NPM yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How do I use Comparator to define a custom sort order?
I am using data table to display car list. Now actually I want to sort the list by car color. Here it is...
Read more >
How to Write Custom Sort Functions in Python
Let's discover how to use custom sort functions to implement custom ... Filter Rows and Select Columns in a Python Data Frame With...
Read more >
Excel SORTBY function - custom sort with formula - Ablebits
See how to use the SORTBY function in Excel to sort data by a custom list, in random order, by text length, and...
Read more >
Table | Sort Comparators | JET Developer Cookbook - Oracle
Department Id Department Name Date 10 Finance 9 2014‑06‑13 20 Control And Credit 9 2019‑09‑10 30 Purchasing 28 2021‑01‑03
Read more >
Data Grid - Sorting - MUI X
Easily sort your rows based on one or several criteria. ... a specific column, you can pass in a custom comparator, and override...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found