"Human-friendly" sorting of mixed numeric and text strings in PostgreSQL
Sorting strings containing mixed numbers and text can be a challenge, especially when the goal is to achieve "human-friendly" sorting. In this case, the number is treated as a single number, not as a whole string.
To solve this problem, a database engineer set out to implement "human-friendly" string sorting in PostgreSQL. The challenge is to split each string into alternating chunks of letters and numbers and then sort the resulting data.
Split string into chunks
One way to split a string into chunks is to use the (D*)(d*)
function with pattern regexp_matches()
. By setting the 'g' option, it returns multiple lines representing a match for each letter and number combination in the given string.
Aggregation of matching results
Next, it is crucial to aggregate these matches into an array. Replacing the empty string ('') with 0 in the integer component (because '' cannot be converted to an integer) ensures that the aggregation can be performed accurately.
Custom composite data type
In order to achieve efficient sorting, engineers created a custom composite data type ai
in the database. This type contains a text field (a) and an integer field (i).
Integrate all steps
The final step is to sort the data based on the constructed array of ai
objects. The ORDER BY
clause first sorts the leading letter components using regexp_replace()
. This helps handle leading numbers and empty strings efficiently. It then sorts an array of ai
objects representing blocks of alternating letters and numbers.
Summary
This approach effectively achieves "human-friendly" string sorting of mixed numbers and text, similar to the behavior observed in Mac OS's Finder. This method combines various PostgreSQL functions to simulate human-style sorting with alternating blocks of letters and numbers, allowing for natural sorting of mixed strings.
The above is the detailed content of How Can We Humanize String Sorting in PostgreSQL for Mixed Numerals and Text?. For more information, please follow other related articles on the PHP Chinese website!