WP_List_Util::sort_callback()privateWP 4.7.0

Callback to sort an array by specific fields.

Method of the class: WP_List_Util{}

No Hooks.

Return

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

Usage

// private - for code of main (parent) class only
$result = $this->sort_callback( $a, $b );
$a(object|array) (required)
One object to compare.
$b(object|array) (required)
The other object to compare.

Notes

Changelog

Since 4.7.0 Introduced.

WP_List_Util::sort_callback() code WP 6.5.2

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;
}