Title: WP_List_Util::sort_callback
Published: December 7, 2016
Last modified: August 20, 2026

---

# WP_List_Util::sort_callback( object|array $a, object|array $b ): int

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?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.

Callback to sort an array by specific fields.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#see-also)󠁿

 * [WP_List_Util::sort()](https://developer.wordpress.org/reference/classes/WP_List_Util/sort/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_list_util/sort_callback/?output_format=md#parameters)󠁿

 `$a`object|arrayrequired

One object to compare.

`$b`object|arrayrequired

The other object to compare.

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

 int 0 if both objects equal. -1 if second object should come first, 1 otherwise.

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

    ```php
    private function sort_callback( $a, $b ) {
    	if ( empty( $this->orderby ) ) {
    		return 0;
    	}

    	$a = (array) $a;
    	$b = (array) $b;

    	foreach ( $this->orderby as $field => $direction ) {
    		if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) {
    			continue;
    		}

    		if ( $a[ $field ] == $b[ $field ] ) {
    			continue;
    		}

    		$results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 );

    		if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) {
    			return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1];
    		}

    		return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1];
    	}

    	return 0;
    }
    ```

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

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

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

## User Contributed Notes

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