Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions templates/docs/examples/patterns/tables/_script-sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down