Callback to sort an array by specific fields.
Description
See also
Parameters
$a
object|arrayrequired- One object to compare.
$b
object|arrayrequired- The other object to compare.
Source
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;
}
Changelog
Version | Description |
---|---|
4.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.