Skip to content

Sorter

The Sorter component provides functionality to sort HTML elements based on their content. It can sort elements in ascending or descending order and supports various data formats including text, numbers, dates, and more.

This component has no external dependencies beyond the core Metro UI library.

<!-- Sort list items -->
<ul data-role="sorter">
<li>Banana</li>
<li>Apple</li>
<li>Orange</li>
<li>Grape</li>
</ul>
<!-- Sort table rows -->
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody data-role="sorter" data-sort-target="tr">
<tr>
<td>Product A</td>
<td>$10.00</td>
</tr>
<tr>
<td>Product B</td>
<td>$5.00</td>
</tr>
<tr>
<td>Product C</td>
<td>$15.00</td>
</tr>
</tbody>
</table>
<!-- Sort numbers -->
<ul data-role="sorter">
<li data-format="number">10</li>
<li data-format="number">2</li>
<li data-format="number">5</li>
</ul>
<!-- Sort dates -->
<ul data-role="sorter">
<li data-format="date">2023-01-15</li>
<li data-format="date">2022-12-25</li>
<li data-format="date">2023-03-01</li>
</ul>
<!-- Sort money values -->
<ul data-role="sorter">
<li data-format="money">$10.50</li>
<li data-format="money">$5.25</li>
<li data-format="money">$15.75</li>
</ul>
<!-- Sort by specific element content -->
<div data-role="sorter" data-sort-source="span.price">
<div>
<h3>Product A</h3>
<span class="price" data-format="money">$10.00</span>
</div>
<div>
<h3>Product B</h3>
<span class="price" data-format="money">$5.00</span>
</div>
<div>
<h3>Product C</h3>
<span class="price" data-format="money">$15.00</span>
</div>
</div>
<!-- Sort in descending order -->
<ul data-role="sorter" data-sort-dir="desc">
<li>Banana</li>
<li>Apple</li>
<li>Orange</li>
</ul>
ParameterTypeDefaultDescription
sorterDeferrednumber0Delay before initialization (in milliseconds)
thousandSeparatorstring”,“Character used as thousand separator for number parsing
decimalSeparatorstring”,“Character used as decimal separator for number parsing
sortTargetstringnullSelector for elements to be sorted. If null, uses the first child element’s tag name
sortSourcestringnullSelector for the element within each item that contains the content to sort by
sortDirstring”asc”Initial sort direction (“asc” or “desc”)
sortStartbooleantrueWhether to sort items on initialization
saveInitialbooleantrueWhether to save the initial order for reset functionality
onSortStartfunctionMetro.noopCallback function when sorting starts
onSortStopfunctionMetro.noopCallback function when sorting ends
onSortItemSwitchfunctionMetro.noopCallback function when items are switched during sorting
onSorterCreatefunctionMetro.noopCallback function when the sorter is created
// Configure global default options for all sorter components
Metro.sorterSetup({
sortDir: "desc",
thousandSeparator: ".",
decimalSeparator: ","
});
  • sort(dir) - Sorts the elements in the specified direction (“asc” or “desc”).
  • reset() - Resets the elements to their initial order.
// Get sorter instance
const sorter = Metro.getPlugin('#mySorter', 'sorter');
// Sort in ascending order
sorter.sort("asc");
// Sort in descending order
sorter.sort("desc");
// Reset to initial order
sorter.reset();

Metro.sorter namespace provides several utility methods:

  • create(el, op) - Creates a new sorter on the specified element with the given options.
  • isSorter(el) - Checks if the specified element is a sorter.
  • sort(el, dir) - Sorts the specified sorter element in the given direction.
  • reset(el) - Resets the specified sorter element to its initial order.
// Sort in ascending order
Metro.sorter.sort('#mySorter', 'asc');
// Reset to initial order
Metro.sorter.reset('#mySorter');
// Check if element is a sorter
const isSorter = Metro.sorter.isSorter('#mySorter');
EventDescription
onSortStartTriggered when sorting begins
onSortStopTriggered when sorting is complete
onSortItemSwitchTriggered when two items are switched during sorting
onSorterCreateTriggered when the sorter component is created

The sorter component can handle various data formats by specifying the data-format attribute on the elements being sorted:

FormatDescriptionExample
dateDate values<li data-format="date">2023-01-15</li>
numberGeneral numbers<li data-format="number">42</li>
intInteger numbers<li data-format="int">42</li>
floatFloating point numbers<li data-format="float">42.5</li>
moneyCurrency values<li data-format="money">$42.50</li>
cardCredit card numbers<li data-format="card">4111 1111 1111 1111</li>
phonePhone numbers<li data-format="phone">(123) 456-7890</li>
AttributeDescription
data-role="sorter"Identifies the element as a sorter component
data-sort-dirSets the sort direction (“asc” or “desc”)
data-sort-targetSpecifies the elements to be sorted
data-sort-sourceSpecifies the element containing the content to sort by
data-formatSpecifies the data format for proper sorting
  1. Always specify the appropriate data-format for non-text content to ensure proper sorting
  2. Use sortSource when you need to sort by a specific part of complex elements
  3. Consider setting sortStart: false if you want to display items in their original order initially
  4. Provide custom thousand and decimal separators if your data uses different formats
  5. Use the reset() method to allow users to return to the original order
  6. For large collections, consider adding loading indicators during sorting operations
  7. When sorting tables, apply the sorter to the tbody element and set sortTarget to “tr”
  8. For better user experience, provide visual indicators of the current sort direction