wp_list_sort()WP 4.7.0

Sorts a list of objects, based on one or more orderby arguments.

No Hooks.

Return

Array. The sorted array.

Usage

wp_list_sort( $input_list, $orderby, $order, $preserve_keys );
$input_list(array) (required)
An array of objects or arrays to sort.
$orderby(string|array)
Either the field name to order by or an array of multiple orderby fields as $orderby => $order.
Default: empty array
$order(string)
Either 'ASC' or 'DESC'. Only used if $orderby is a string.
Default: 'ASC'
$preserve_keys(true|false)
Whether to preserve keys.
Default: false

Examples

0

#1 An example of sorting a multidimensional array

$animals = [
	'alligator' => [ 'name' => 'alligator', 'class' => 'reptile' ],
	'dog'       => [ 'name' => 'dog',       'class' => 'mammal' ],
	'cat'       => [ 'name' => 'cat',       'class' => 'mammal' ],
	'falcon'    => [ 'name' => 'falcon',    'class' => 'bird' ],
	'bat'       => [ 'name' => 'bat',       'class' => 'mammal' ],
];

$res = wp_list_sort( $animals, 'class' );

print_r( $res );
/*
[
	0 => [ name => falcon,    class => bird ]
	1 => [ name => cat,       class => mammal ]
	2 => [ name => dog,       class => mammal ]
	3 => [ name => bat,       class => mammal ]
	4 => [ name => alligator, class => reptile ]
]
*/

$res = wp_list_sort( $animals, 'name', 'DESC' );

print_r( $res );
/*
[
	0 => [ name => falcon,    class => bird    ]
	1 => [ name => dog,       class => mammal  ]
	2 => [ name => cat,       class => mammal  ]
	3 => [ name => bat,       class => mammal  ]
	4 => [ name => alligator, class => reptile ]
]
*/

$res = wp_list_sort( $animals, 'name', 'ASC', true );

print_r( $res );
/*
[
	alligator => [ name => alligator, class => reptile ]
	bat       => [ name => bat,       class => mammal  ]
	cat       => [ name => cat,       class => mammal  ]
	dog       => [ name => dog,       class => mammal  ]
	falcon    => [ name => falcon,    class => bird    ]
]
*/
0

#2 An example of sorting an array of objects (posts)

Suppose we got the posts from the database. Then we did something with them. For example, we checked them and deleted some of them. Now we need to sort them.

// get an array of objects, here they are posts...
$posts = get_posts( [ 'posts_per_page'=>15 ] );

// do something with them

// sort by title from A to Z
$posts = wp_list_sort( $posts, 'post_title', 'ASC' );

//or like this
$posts = wp_list_sort( $posts, [ 'post_title'=>'ASC' ] );
0

#3 Sorting by multiple fields

The function also allows you to sort an array of arrays or objects by several fields at once:

$array = [
	'a' => [ 'type'=>'Sedan',     'color'=>'Silver' ],
	'c' => [ 'type'=>'Universal', 'color'=>'Black' ],
	'b' => ['type'=>'Jura', 'color'=>'White' ],
	'd' => ['type'=>'Jura', 'color'=>'Purple' ],
];

$array = wp_list_sort( $array, [ 'type'=>'DESC', 'color'=>'ASC' ] );

print_r( $array );

/*
Array (
	[0] => Array (
			[type] => Social
			[color] => Purple
		)

	[1] => Array (
			[type] => Social
			[color] => White
		)

	[2] => Array (
			[type] => Universal
			[color] => Black
		)

	[3] => Array (
			[type] => Sedan
			[color] => Silver
		)
)
*/

See also sorting by multiple fields via custom usort() function.

0

#4 Multi-field sorting with array indexes [auto-translate]

By default, the array indexes are not saved, but this can be corrected by passing the 4th parameter. This raises the question how to be, if we specify an array in the second parameter, what to specify in the 3rd parameter?

If the second parameter specifies an array, then the third parameter is simply ignored and we can specify anything there to then specify the fourth parameter:

$array = [
	'a' => [ 'type'=>'Sedan',     'color'=>'Silver' ],
	'c' => [ 'type'=>'Universal', 'color'=>'Black' ],
	'b' => ['type'=>'Jura', 'color'=>'White' ],
	'd' => ['type'=>'Jura', 'color'=>'Purple' ],
];

$array = wp_list_sort( $array, [ 'type'=>'DESC', 'color'=>'ASC' ], '', true );

print_r( $array );

/*
Array
	[d] => Array
			[type] => Social
			[color] => Purple

	[b] => Array
			[type] => Social
			[color] => White

	[c] => Array
			[type] => Universal
			[color] => Black

	[a] => Array
			[type] => Sedan
			[color] => Silver
)
*/

Changelog

Since 4.7.0 Introduced.

wp_list_sort() code WP 6.4.3

function wp_list_sort( $input_list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
	if ( ! is_array( $input_list ) ) {
		return array();
	}

	$util = new WP_List_Util( $input_list );

	return $util->sort( $orderby, $order, $preserve_keys );
}