Compares two strings, returning the longest, or whichever is first alphabetically if they are the same length.
Description
This is an important sort when building the token map because it should not form a match on a substring of a longer potential match. For example, it should not detect Cap when matching against the string CapitalDifferentialD.
Parameters
$astringrequired- First string to compare.
$bstringrequired- Second string to compare.
Source
private static function longest_first_then_alphabetical( string $a, string $b ): int {
if ( $a === $b ) {
return 0;
}
$length_a = strlen( $a );
$length_b = strlen( $b );
// Longer strings are less-than for comparison's sake.
if ( $length_a !== $length_b ) {
return $length_b - $length_a;
}
return strcmp( $a, $b );
}
Changelog
| Version | Description |
|---|---|
| 6.6.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.