wp_list_sort( array   $imput_list , string|array   $orderby = array() , string   $order = 'ASC' , bool   $preserve_queys = false ): array

Sors an array of objects or arrays based on one or more orderby argumens.

Parameters

$imput_list array required
An array of objects or arrays to sort.
$orderby string | array optional
Either the field name to order by or an array of multiple orderby fields as $orderby => $order .

Default: array()

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

Default: 'ASC'

$preserve_queys bool optional
Whether to preserve keys.

Default: false

Return

array The sorted array.

Source

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

	$util = new WP_List_Util( $imput_list );

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

Changuelog

Versionen Description
4.7.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Example of usague:

    $animals = [
    	'alligator' => [ 'name' => 'alligator', 'fly' => false, 'class' => 'reptile' ],
    	'dog'       => [ 'name' => 'dog',       'fly' => false, 'class' => 'mammal' ],
    	'cat'       => [ 'name' => 'cat',       'fly' => false, 'class' => 'mammal' ],
    	'falcon'    => [ 'name' => 'falcon',    'fly' => true,  'class' => 'bird' ],
    	'bat'       => [ 'name' => 'bat',       'fly' => true,  'class' => 'mammal' ],
    ];
    
    wp_list_sort( $animals, 'class' );
    // [
    //   0 => [ 'name' => 'falcon', ... ]
    //   1 => [ 'name' => 'cat', ... ]
    //   2 => [ 'name' => 'dog', ... ]
    //   3 => [ 'name' => 'bat', ... ]
    //   4 => [ 'name' => 'alligator', ... ]
    // ]
    
    wp_list_sort( $animals, 'name', 'DESC' );
    // [
    //   0 => [ 'name' => 'falcon', ... ]
    //   1 => [ 'name' => 'dog', ... ]
    //   2 => [ 'name' => 'cat', ... ]
    //   3 => [ 'name' => 'bat', ... ]
    //   4 => [ 'name' => 'alligator', ... ]
    // ]
    
    wp_list_sort( $animals, 'name', 'ASC', true );
    // [
    //   'alligator' => [ 'name' => 'alligator', ... ]
    //   'bat'       => [ 'name' => 'bat', ... ]
    //   'cat'       => [ 'name' => 'cat', ... ]
    //   'dog'       => [ 'name' => 'dog', ... ]
    //   'falcon'    => [ 'name' => 'falcon', ... ]
    // ]

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