WP_List_Util::sort( string|array $orderby = array(), string $order = ‘ASC’, bool $preserve_keys = false ): array

In this article

Sorts the input array based on one or more orderby arguments.

Parameters

$orderbystring|arrayoptional
Either the field name to order by or an array of multiple orderby fields as $orderby => $order.

Default:array()

$orderstringoptional
Either 'ASC' or 'DESC'. Only used if $orderby is a string. Default 'ASC'.

Default:'ASC'

$preserve_keysbooloptional
Whether to preserve keys.

Default:false

Return

array The sorted array.

Source

public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
	if ( empty( $orderby ) ) {
		return $this->output;
	}

	if ( is_string( $orderby ) ) {
		$orderby = array( $orderby => $order );
	}

	foreach ( $orderby as $field => $direction ) {
		$orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC';
	}

	$this->orderby = $orderby;

	if ( $preserve_keys ) {
		uasort( $this->output, array( $this, 'sort_callback' ) );
	} else {
		usort( $this->output, array( $this, 'sort_callback' ) );
	}

	$this->orderby = array();

	return $this->output;
}

Changelog

VersionDescription
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.