Problem/Motivation
In #3065903: Add label sort ability to Select element, we add two options to the select form element:
-
#sort_options: Set to TRUE (default is FALSE) to sort the options by their (translated) labels. -
#sort_start: Set to an integer (default is 0) to start the sorting at that index in the labels. For instance, if your first option is "- none -", start the sorting at index 1 to make sure that first option stays at the top of the list of options.
We propose adding additional, related options. Two such options were suggested in comments on that issue.
Proposed resolution
Add related options:
-
#sort_direction: Set to 'ASC' (default) for ascending order, 'DESC' for descending order. -
#sort_natural: Set to FALSE (default) to sort usingstrcmp(), TRUE to sort usingstrnatcmp(). -
#sort_end: Integer for number of elements at the end to leave at the end, or maybe the element number to stop sorting at (TBD which is best -- probably easiest to say "leave 1 element at the end" which is the usual use case. -
#sort_unique: Set to TRUE (default is FALSE) to remove duplicates after sorting. Two options are considered duplicates if they have the samevalueattribute and text value (label). This option could be used in #3089528: Sort options for entity reference fields can be ambiguous if a field has different labels across bundles.
See Natural sort order (Wikipedia) and strnatcmp (PHP docs) for the differences between strcmp() and strnatcmp().
Remaining tasks
Decide whether these options are useful enough that they should be added to Drupal core. (There is already some discussion of this question on #3065903: Add label sort ability to Select element.)
Decide whether to change the names and/or allowed values of these options. For example, if there are useful comparison functions other than strcmp() and strnatcmp(), then we might replace the boolean sort_natural option with a string-valued option.
Decide whether there are other related options worth adding.
Decide whether there are other places in core where strnatcmp() is a useful alternative.
User interface changes
None.
API changes
New options will be added to the select element in the Form API.
Data model changes
None.
Release notes snippet
TODO.
Comments
Comment #2
jhodgdonAdding sort_end suggestion from that other issue.
Comment #4
benjifisherI am adding the
sort_uniqueoption, as suggested on #3089528: Sort options for entity reference fields can be ambiguous if a field has different labels across bundles.Comment #5
benjifisherClarify what "duplicates" means.
Comment #6
cirdan13 commentedI have a use case for this feature as a french user : I have a list of countries in a select, and they may contain accents, but I want (and I think it's a common need in any language) the list sorted using transliteration, i.e. ignoring accents.
I think it's an extension of the "sort_natural" flag.
Example :
[Swaziland, Sénégal] should be sorted as [Sénégal,Swaziland]
It's my first "contribution" to Drupal, so I'm not familiar with the way of doing things here, but I may help if you help me helping ;-)
I though that an interesting thing would maybe be to allow the user to provide a custom sorting function as a closure.
Comment #14
solideogloria commentedPerhaps a
#sort_callbackwould make it all easier? That would also handle custom cases.