Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness. Use WP_List_Util::sort() instead.

WP_List_Util::sort_callback() WordPress Method

The WP_List_Util::sort_callback() method is used to sort a list of objects by a specified field. The field can be specified using a dot notation (i.e. field.subfield).

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

Callback to sort an array by specific fields.


Description

Top ↑

See also


Top ↑

Parameters

$a

(object|array)(Required)One object to compare.

$b

(object|array)(Required)The other object to compare.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/class-wp-list-util.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;
	}

Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.