From 7cf34208088cb9f851690a1a37708d7fde07762d Mon Sep 17 00:00:00 2001 From: Mashrukh Islam Date: Sun, 19 Jul 2026 20:36:51 +0530 Subject: [PATCH] fix: fix table sorting for numeric and alphanumeric values --- .../docs/examples/patterns/tables/_script-sorting.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/templates/docs/examples/patterns/tables/_script-sorting.js b/templates/docs/examples/patterns/tables/_script-sorting.js index 203ac1742..f47ea6e33 100644 --- a/templates/docs/examples/patterns/tables/_script-sorting.js +++ b/templates/docs/examples/patterns/tables/_script-sorting.js @@ -45,18 +45,17 @@ function sortTable(header, table) { return a.getAttribute('data-index') - b.getAttribute('data-index'); }); } else { - // Sort based on a cell contents + // Enables natural alphanumeric sorting (e.g., "2 GiB" comes before "3 GiB") + var collator = new Intl.Collator(undefined, ({numeric: true, sensitivity: "base"})) + newRows.sort(function (rowA, rowB) { // Trim the cell contents. var contentA = rowA.cells[col].textContent.trim(); var contentB = rowB.cells[col].textContent.trim(); + // Compares the strings naturally and applies the sorting direction (ascending/descending) // Based on the direction, do the sort. - // - // This example only sorts based on alphabetical order, to sort based on - // number value a more specific implementation would be needed, to provide - // number parsing and comparison function between text strings and numbers. - return contentA < contentB ? direction : -direction; + return collator.compare(contentA, contentB) * direction; }); } // Append each row into the table, replacing the current elements.