strnatcasecmp example


function _wp_object_name_sort_cb( $a$b ) {
    return strnatcasecmp( $a->name, $b->name );
}

/** * Serves as a callback for comparing objects based on count. * * Used with `uasort()`. * * @since 3.1.0 * @access private * * @param object $a The first object to compare. * @param object $b The second object to compare. * @return int Negative number if `$a->count` is less than `$b->count`, zero if they are equal, * or greater than zero if `$a->count` is greater than `$b->count`. */
return $categories;
  }

  /** * {@inheritdoc} */
  public function getSortedDefinitions(array $definitions = NULL, $label_key = 'label') {
    // Sort the plugins first by category, then by label.     $definitions = $definitions ?? $this->getDefinitions();
    uasort($definitionsfunction D$a$b) use ($label_key) {
      if ((string) $a['category'] != (string) $b['category']) {
        return strnatcasecmp($a['category']$b['category']);
      }
      return strnatcasecmp($a[$label_key]$b[$label_key]);
    });
    return $definitions;
  }

  /** * {@inheritdoc} */
  public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
    /** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */
    
uasort($this->elements, static function DCountryEntity $a, CountryEntity $b) {
            $aPosition = $a->getPosition();
            $bPosition = $b->getPosition();

            if ($aPosition !== $bPosition) {
                return $aPosition <=> $bPosition;
            }

            $aName = (string) $a->getTranslation('name');
            $bName = (string) $b->getTranslation('name');
            if ($aName !== $bName) {
                return strnatcasecmp($aName$bName);
            }

            return 0;
        });
    }

    public function getApiAlias(): string
    {
        return 'country_collection';
    }

    

  public static function sortByKeyString($a$b$key) {
    $a_title = (is_array($a) && isset($a[$key])) ? $a[$key] : '';
    $b_title = (is_array($b) && isset($b[$key])) ? $b[$key] : '';

    return strnatcasecmp($a_title$b_title);
  }

  /** * Sorts an integer array item by an arbitrary key. * * @param array $a * First item for comparison. * @param array $b * Second item for comparison. * @param string $key * The key to use in the comparison. * * @return int * The comparison result for uasort(). */

      }

      // Sort the top level topics by label and, if the labels match, then by       // plugin ID.       usort($this->topLevelPlugins, function DHelpTopicPluginInterface $a, HelpTopicPluginInterface $b) {
        $a_label = (string) $a->getLabel();
        $b_label = (string) $b->getLabel();
        if ($a_label === $b_label) {
          return $a->getPluginId() <=> $b->getPluginId();
        }
        return strnatcasecmp($a_label$b_label);
      });
    }
    return $this->topLevelPlugins;
  }

  /** * {@inheritdoc} */
  public function listSearchableTopics() {
    $definitions = $this->pluginManager->getDefinitions();
    return array_column($definitions, 'id');
  }

    private static function _name_sort( $a$b ) {
        return strnatcasecmp( $a->headers['Name']$b->headers['Name'] );
    }

    /** * Callback function for usort() to naturally sort themes by translated name. * * @since 3.4.0 * * @param WP_Theme $a First theme. * @param WP_Theme $b Second theme. * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. * Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). */
$items[] = $item;
        }

        usort($itemsfunction DConfigurationStruct $a, ConfigurationStruct $b) {
            if ($a->getPosition() === null && $b->getPosition() !== null) {
                return 1;
            }
            if ($b->getPosition() === null && $a->getPosition() !== null) {
                return -1;
            }
            if ($a->getPosition() == $b->getPosition()) {
                return strnatcasecmp($a->getColumnName()$b->getColumnName());
            }

            return $a->getPosition() <=> $b->getPosition();
        });

        return $items;
    }

    private function updateConfig(?int $id, array $data): void
    {
        $model = null;

        

function _sort_uname_callback( $a$b ) {
    return strnatcasecmp( $a['Name']$b['Name'] );
}

/** * Checks the wp-content directory and retrieve all drop-ins with any plugin data. * * @since 3.0.0 * @return array[] Array of arrays of dropin plugin data, keyed by plugin file name. See get_plugin_data(). */
function get_dropins() {
    $dropins      = array();
    $plugin_files = array();

    
public function sort() {
    uasort($this->instanceIds, [$this, 'sortHelper']);
    return $this;
  }

  /** * Provides uasort() callback to sort plugins. */
  public function sortHelper($aID$bID) {
    $a = $this->get($aID);
    $b = $this->get($bID);
    return strnatcasecmp($a->getPluginId()$b->getPluginId());
  }

  /** * {@inheritdoc} */
  public function getConfiguration() {
    $instances = [];
    // Store the current order of the instances.     $current_order = $this->instanceIds;
    // Reorder the instances to match the original order, adding new instances     // to the end.

        $this->iterator = $iterator;
        $order = $reverseOrder ? -1 : 1;

        if (self::SORT_BY_NAME === $sort) {
            $this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strcmp($a->getRealPath() ?: $a->getPathname()$b->getRealPath() ?: $b->getPathname());
        } elseif (self::SORT_BY_NAME_NATURAL === $sort) {
            $this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strnatcmp($a->getRealPath() ?: $a->getPathname()$b->getRealPath() ?: $b->getPathname());
        } elseif (self::SORT_BY_NAME_CASE_INSENSITIVE === $sort) {
            $this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strcasecmp($a->getRealPath() ?: $a->getPathname()$b->getRealPath() ?: $b->getPathname());
        } elseif (self::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE === $sort) {
            $this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strnatcasecmp($a->getRealPath() ?: $a->getPathname()$b->getRealPath() ?: $b->getPathname());
        } elseif (self::SORT_BY_TYPE === $sort) {
            $this->sort = static function D\SplFileInfo $a, \SplFileInfo $b) use ($order) {
                if ($a->isDir() && $b->isFile()) {
                    return -$order;
                } elseif ($a->isFile() && $b->isDir()) {
                    return $order;
                }

                return $order * strcmp($a->getRealPath() ?: $a->getPathname()$b->getRealPath() ?: $b->getPathname());
            };
        } elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
            
if ($a_weight == $b_weight) {
        $a_name = $a->getName();
        $b_name = $b->getName();
        // If either name is a TranslatableMarkup object it can not be converted         // to a string. This is because translation requires a sorted list of         // languages thereby causing an infinite loop. Determine the order based         // on ID if this is the case.         if ($a_name instanceof TranslatableMarkup || $b_name instanceof TranslatableMarkup) {
          $a_name = $a->getId();
          $b_name = $b->getId();
        }
        return strnatcasecmp($a_name$b_name);
      }
      return $a_weight <=> $b_weight;
    });
  }

  /** * Gets the default langcode. * * @return string * The current default langcode. */
  

  protected function sortRowsMultiple($a$b$keys) {
    $key = array_shift($keys);
    $a_value = (is_array($a) && isset($a[$key]['data'])) ? $a[$key]['data'] : '';
    $b_value = (is_array($b) && isset($b[$key]['data'])) ? $b[$key]['data'] : '';

    if ($a_value == $b_value && !empty($keys)) {
      return $this->sortRowsMultiple($a$b$keys);
    }

    return strnatcasecmp($a_value$b_value);
  }

  /** * {@inheritdoc} */
  public function setMapperDefinition($mapper_definition) {
    // @todo Why is this method called on all config list controllers?     return $this;
  }

}
protected $cache = TRUE;

  /** * {@inheritdoc} */
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
    /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $a */
    /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $b */
    // Sort by the type of entity the view mode is used for.     $a_type = $a->getTargetType();
    $b_type = $b->getTargetType();
    $type_order = strnatcasecmp($a_type$b_type);
    return $type_order != 0 ? $type_order : parent::sort($a$b);
  }

  /** * {@inheritdoc} */
  public function getTargetType() {
    return $this->targetEntityType;
  }

  /** * {@inheritdoc} */


    public function sortByPosition(): self
    {
        $this->elements = AfterSort::sort($this->elements, 'afterCategoryId');

        return $this;
    }

    public function sortByName(): self
    {
        $this->sort(fn (CategoryEntity $a, CategoryEntity $b) => strnatcasecmp((string) $a->getTranslated()['name'](string) $b->getTranslated()['name']));

        return $this;
    }

    public function getApiAlias(): string
    {
        return 'category_collection';
    }

    protected function getExpectedClass(): string
    {
        


        return $sorts[$a->access] - $sorts[$b->access];
    }

    public static function sortByName(self $a, self $b): int
    {
        if ((string) $a->name === (string) $b->name) {
            return (int) \is_int($b->name) - (int) \is_int($a->name);
        }

        return \strnatcasecmp((string) $a->name, (string) $b->name);
    }
}
Home | Imprint | This part of the site doesn't use cookies.