Anemone::sort()

Sorts the data.

Table of Contents
  1. Description
  2. Example

Description

Anemone::sort(array $order = [int $sort, string $by], bool $keys = false): self;
Anemone::sort(callable $fn, bool $keys = false): self;
Anemone::sort(int $sort = 1, bool $keys = false): self;

This method sorts the data based on the value of a column on every item in the data.

Example

$anemone = new Anemone([
    'foo' => [
        'size' => 'medium',
        'color' => 'green',
        'id' => 2
    ],
    'bar' => [
        'id' => 4
    ],
    'baz' => [
        'size' => 'large',
        'color' => 'blue',
        'id' => 3
    ],
    'qux' => [
        'size' => 'small',
        'color' => 'red',
        'id' => 1
    ]
]);

test($anemone->mitose()->sort(+1)->get()); // Sorts items ascending (does not work properly on associative array data)
test($anemone->mitose()->sort([+1, 'color'], true)->get()); // Sorts items ascending by `color` value
test($anemone->mitose()->sort(function ($a, $b) {
    $a = $a['color'][1] ?? "";
    $b = $b['color'][1] ?? "";
    return $a <=> $b;
}, true)->get()); // Sorts items ascending by the second letter of `color` value

You call the Anemone::get() method to return raw array values, but you can also directly iterate over it.

Anemone::any()

Checks if at least one item in the data passes the test.

Anemone::is()

Filters the data so that only items that pass the test are left.

Anemone::not()

Filters the data so that only items that do not pass the test are left.

Anemone::pluck()

Returns a new data set contains values from the key on every item.

Anemone::sort()

Sorts the data.