The set of all useful comparison functions is very large. I have used dozens of distinct ordering criteria with various sort utilities over the years. I would gladly have used more had the sort options been more flexible. I have written more comparison functions in C than I care to count. It is clearly hard to guess the parameters you need for a flexible parametric comparison function. It is also onerous to require users to specify comparison functions by writing C code.
Both those problems came back to haunt me when writing a book on the Standard C library. One interesting feature added to C is the concept of locale-dependent collation. In other words, you can specify how to compare two text strings using ordering rules that vary across cultures (and subcultures).
The workhorse function is called strcoll. You call it with pointers to two null-terminated strings. It returns the usual three-way value you need to order strings by pairwise comparisons. If you expect to repeat such comparisons often, another function can speed the process. You call strxfrm to translate a string into an alternate form. Byte-by-byte comparisons of two such transformed strings yields the same ordering as calling strcoll with the untransformed strings.
The C Standard says an implementation must contain the functions strcoll and strxfrm. It says changing the locale category LC_COLLATE can change the behavior of these functions. But that's all it says. It gives no hints about:
- How to specify various "collations"
- What names to give them
- How to implement the functions
- How to change their behavior when the locale changes.
Those were the issues I tackled when I chose to implement a complete version of the Standard C library. My approach to locales was to introduce a locale file, which is a text file you can prepare with your favorite text editor. You use it to describe one or more locales. Each specifies a number of culture-dependent parameters.