WP_List_Util::sort() WordPress Method

The WP_List_Util::sort() method is used to sort a list of objects. This is a static method and it accepts two arguments. The first argument is the array of objects to be sorted. The second argument is an optional comparison function. The default comparison function is the built-in PHP function usort().

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

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


Parameters

$orderby

(string|array)(Optional) Either the field name to order by or an array of multiple orderby fields as $orderby => $order.

Default value: array()

$order

(string)(Optional) Either 'ASC' or 'DESC'. Only used if $orderby is a string.

Default value: 'ASC'

$preserve_keys

(bool)(Optional) Whether to preserve keys.

Default value: false


Top ↑

Return

(array) The sorted array.


Top ↑

Source

File: wp-includes/class-wp-list-util.php

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

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.