Complex Sorting Issue in MRT with Alphanumeric, Range, and Unit-based Values (e.g., μ, kHz, ℃ etc) #1246
Unanswered
KavitatheGupta
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Issue:
I am experiencing an issue with sorting alphanumeric values containing units in Material React Table (MRT). Specifically, the table contains values with micron (μ) and kilo (k) units, and the default lexicographical sorting leads to incorrect results.
For example, when trying to sort values such as "1.02k" and "1.2k" or "0.01μ" and "0.1μ", the lexicographical comparison treats these values as strings, which produces incorrect sorting results.
micron (μ) unit example:
"0.01μ"
"0.1μ"
Expected result (ascending):
0.01μ
0.01μ
0.01μ
0.1μ
0.1μ
0.1μ
Actual result:
0.01μ
0.1μ
0.01μ
0.1μ
0.01μ
0.1μ
Problem: the lexicographical sorting treats "0.1μ" as smaller than "0.01μ" after comparing "1" and "0" in the string. This results in "0.1μ" being placed incorrectly before "0.01μ", despite the numeric value 0.01 being smaller.
Challenges:
In my dataset, there are various units such as micron (μ), kilo (k), and other unit notations, which makes it impractical to implement custom sorting logic for each specific unit. The table also includes values with ranges (e.g., "4.5~5.5") and complex expressions (e.g., "6800μ(1kHz)").
The issue arises because the default sorting in MRT is lexicographical, and it doesn't handle numeric sorting for such alphanumeric values. Ideally, the numeric component should be sorted first, and the units should be ignored or treated separately.
Steps to Reproduce:
Create a column in MRT with values like "1.2k", "1.02k", "0.01μ", and "0.1μ".
Enable default sorting in ascending order.
Observe the incorrect order in which the values are sorted.
Expected Behavior:
The sorting algorithm should:
Handle the numeric part of the alphanumeric values, ensuring that "1.02k" is sorted before "1.2k" and "0.01μ" is sorted before "0.1μ".
The unit suffix (e.g., k, μ) should either be ignored during sorting or treated in a way that does not interfere with numeric comparison.
Questions:
Is there a built-in way to handle numeric sorting for alphanumeric values with units in MRT?
How can I customize the sorting logic to treat the numeric component of the values first, ignoring the unit suffix (e.g., μ, k)?
Beta Was this translation helpful? Give feedback.
All reactions