views_cast_sort
The problem
Drupal Views sorts string fields alphabetically by default. When those strings contain numbers the result looks wrong:
- Product 1
- Product 10
- Product 101
- Product 15
- Product 2
This module fixes that by applying SQL type casting directly in the ORDER BY clause; no pre-computed index tables, no custom SQL required from site builders.
Two sort handlers
Cast sort wraps the field in CAST(field AS type) before sorting. Use this for fields that store purely numeric values as strings: SKUs, reference numbers, serial numbers, rankings. Supports integer, decimal, date, datetime, and text cast types, configurable in the Views UI.
Trailing integer sort uses REGEXP_SUBSTR(field, '[0-9]+$') to extract the integer at the end of each value before casting. Use this for mixed strings such as "Product 10", "SKU-ref-207", or "Item 3B".
Requirements
Drupal 10 or 11
Trailing integer sort requires MySQL 8.0.4+ or MariaDB 10.0.5+. Cast sort works on all supported Drupal databases. An in-form warning and watchdog entry are shown if the database does not meet the requirement.
Relation to similar modules
Views Natural Sort solves a related problem using a pre-computed index approach. Views Cast Sort is a lighter alternative for the common case where you want numeric ordering of a string field without maintaining a separate index.