Skip to content

Kirby 5.6.1

A::sort()

Sorts a multi-dimensional array by a certain column

A::sort(array $array, string $field, string $direction = 'desc', int $method = 0): array

Parameters

Name Type Default Description
$arrayrequired array no default value The source array
$fieldrequired string no default value The name of the column
$direction string 'desc' desc (descending) or asc (ascending)
$method int 0 A PHP sort method flag or 'natural' for
natural sorting, which is not supported in
PHP by sort flags

Return type

array

The sorted array

Parent class

Kirby\Toolkit\A

Examples

$array[0] = [
  'id' => 1,
  'username' => 'mike',
];

$array[1] = [
  'id' => 2,
  'username' => 'peter',
];

$array[3] = [
  'id' => 3,
  'username' => 'john',
];

$sorted = A::sort($array, 'username ASC');
// Array
// (
//      [0] => Array
//          (
//              [id] => 3
//              [username] => john
//          )
//      [1] => Array
//          (
//              [id] => 1
//              [username] => mike
//          )
//      [2] => Array
//          (
//              [id] => 2
//              [username] => peter
//          )
// )