function NaturalSort::strnatcasecmp

Compares strings using a language-aware "natural order" algorithm.

This method is a drop-in replacement for strnatcasecmp(). If the intl extension is available, strings are compared using a \Collator for the given language, so accented characters and other language-specific collation rules are taken into account. Numbers embedded in the strings are compared by their numeric value, as strnatcasecmp() does. If the intl extension is not available, or no language can be determined, this method falls back to strnatcasecmp().

Parameters

string $string1: The first string.

string $string2: The second string.

string|null $langcode: (optional) The language code to determine the collation rules. Defaults to the current language if the language manager service is available.

Return value

int Less than 0 if $string1 sorts before $string2, greater than 0 if $string1 sorts after $string2, and 0 if they are equal.

9 calls to NaturalSort::strnatcasecmp()
Action::sort in core/modules/system/src/Entity/Action.php
Block::sort in core/modules/block/src/Entity/Block.php
Sorts active blocks by weight; sorts inactive blocks by name.
ConfigEntityBase::sort in core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
Callback for uasort() to sort configuration entities by weight and label.
DateFormat::sort in core/lib/Drupal/Core/Datetime/Entity/DateFormat.php
EntityDisplayModeBase::sort in core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php

... See full list

File

core/lib/Drupal/Core/Utility/NaturalSort.php, line 42

Class

NaturalSort
Provides a language-aware replacement for strnatcasecmp().

Namespace

Drupal\Core\Utility

Code

public static function strnatcasecmp(string $string1, string $string2, ?string $langcode = NULL) : int {
  if (!extension_loaded('intl')) {
    return strnatcasecmp($string1, $string2);
  }
  if ($langcode === NULL && \Drupal::hasService('language_manager')) {
    $langcode = \Drupal::languageManager()->getCurrentLanguage()
      ->getId();
  }
  if ($langcode === NULL) {
    return strnatcasecmp($string1, $string2);
  }
  if (!isset(self::$collators[$langcode])) {
    $collator = \Collator::create($langcode);
    if ($collator === NULL) {
      return strnatcasecmp($string1, $string2);
    }
    // Compare numbers embedded in the strings by their numeric value, so
    // that for example "10" sorts after "9".
    $collator->setAttribute(\Collator::NUMERIC_COLLATION, \Collator::ON);
    self::$collators[$langcode] = $collator;
  }
  return self::$collators[$langcode]->compare($string1, $string2);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.