Skip to content

Add SI-prefix-aware sorting column for parts tableFeature/si value sort#1344

Open
wschopohl wants to merge 3 commits intoPart-DB:masterfrom
wschopohl:feature/si-value-sort
Open

Add SI-prefix-aware sorting column for parts tableFeature/si value sort#1344
wschopohl wants to merge 3 commits intoPart-DB:masterfrom
wschopohl:feature/si-value-sort

Conversation

@wschopohl
Copy link
Copy Markdown

Summary

  • Adds an optional "SI Value" column to the parts table that parses numeric values with SI prefixes (p, n, u/µ, m, k/K, M, G, T) from part names and sorts by the resulting physical value
  • Useful for electronic components where alphabetical sorting produces wrong results — e.g. 100nF, 10pF, 1uF sort as 10pF < 100nF < 1uF
  • The column displays the parsed float value, or an empty cell for non-matching names
  • Only matches numbers at the start of the name — part numbers like "MCP2515" or "Crystal 20MHz" are not affected
  • Supports both dot and comma decimal separators (European locale)
  • Falls back to NATSORT for parts without SI values, so they sort alphabetically rather than arbitrarily
  • Opt-in: not in default columns, users enable it via the column visibility button

Implementation

  • New SiValueSort DQL function with platform-specific SQL for PostgreSQL (POSIX regex), MySQL/MariaDB (REGEXP_SUBSTR), and SQLite (PHP callback via the existing middleware)
  • 33 unit tests covering all platforms, SI prefixes, edge cases, and sort order verification

Test plan

  • Unit tests pass (33 tests, all platforms)
  • PHPStan level 2 passes
  • Tested against real parts database with SQLite

Adds an optional "Name (SI)" column that parses numeric values with SI
prefixes (p, n, u/µ, m, k/K, M, G, T) from part names and sorts by the
resulting physical value. This is useful for electronic components where
alphabetical sorting produces wrong results — e.g. 100nF, 10pF, 1uF
should sort as 10pF < 100nF < 1uF.

Implementation:
- New SiValueSort DQL function with platform-specific SQL generation
  for PostgreSQL (POSIX regex), MySQL/MariaDB (REGEXP_SUBSTR), and
  SQLite (PHP callback registered via the existing middleware).
- The regex is start-anchored: only names beginning with a number are
  matched. Part numbers like "MCP2515" or "Crystal 20MHz" are ignored.
- When SI sort is active, NATSORT is appended as a secondary sort so
  that non-matching parts fall back to natural string ordering instead
  of appearing in arbitrary order.
- The column is opt-in (not in default columns) and displays the parsed
  float value, or an empty cell for non-matching names.
The column now shows the parsed numeric value rather than the part name,
so the label should reflect that.
Part names using European decimal notation (e.g. "4,7 kΩ", "2,2uF")
were parsed incorrectly because the regex only recognized dots. Now
commas are normalized to dots before parsing, matching the existing
pattern used elsewhere in the codebase (PartNormalizer, price providers).
@wschopohl
Copy link
Copy Markdown
Author

SI Value Demo

For completeness, here is a screenshot showing how SI Value improves table ordering. It uses the value from 'Name' under the hood, extracting leading numbers with SI prefixes.

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.

1 participant