Title: WP_Token_Map::longest_first_then_alphabetical
Published: July 16, 2024
Last modified: August 20, 2026

---

# WP_Token_Map::longest_first_then_alphabetical( string $a, string $b ): int

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#changelog)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Compares two strings, returning the longest, or whichever is first alphabetically
if they are the same length.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#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](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#parameters)󠁿

 `$a`stringrequired

First string to compare.

`$b`stringrequired

Second string to compare.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#return)󠁿

 int -1 or lower if `$a` is less than `$b`; 1 or greater if `$a` is greater than`
$b`, and 0 if they are equal.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#source)󠁿

    ```php
    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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-token-map.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-token-map.php#L817)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-token-map.php#L817-L831)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Token_Map::from_array()](https://developer.wordpress.org/reference/classes/wp_token_map/from_array/)`wp-includes/class-wp-token-map.php` |

Create a token map using an associative array of key/value pairs as the input.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_token_map/longest_first_then_alphabetical/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.6.0](https://developer.wordpress.org/reference/since/6.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_token_map%2Flongest_first_then_alphabetical%2F)
before being able to contribute a note or feedback.