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:

  1. #sort_direction: Set to 'ASC' (default) for ascending order, 'DESC' for descending order.
  2. #sort_natural: Set to FALSE (default) to sort using strcmp(), TRUE to sort using strnatcmp().
  3. #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.
  4. #sort_unique: Set to TRUE (default is FALSE) to remove duplicates after sorting. Two options are considered duplicates if they have the same value attribute 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

benjifisher created an issue. See original summary.

jhodgdon’s picture

Issue summary: View changes

Adding sort_end suggestion from that other issue.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

benjifisher’s picture

benjifisher’s picture

Issue summary: View changes

Clarify what "duplicates" means.

cirdan13’s picture

I 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.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

solideogloria’s picture

Perhaps a #sort_callback would make it all easier? That would also handle custom cases.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.