Title: WP_List_Util::sort
Published: December 7, 2016
Last modified: May 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#wp--skip-link--target)

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#parameters)󠁿

 `$orderby`string|arrayoptional

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

Default:`array()`

`$order`stringoptional

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

Default:`'ASC'`

`$preserve_keys`booloptional

Whether to preserve keys.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#return)󠁿

 array The sorted array.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-list-util.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/class-wp-list-util.php#L229)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-list-util.php#L229-L253)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_list_util/sort/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_list_util%2Fsort%2F)
before being able to contribute a note or feedback.