1. Go to this page and download the library: Download stratadox/sorting library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Stratadox\Sorting\ObjectSorter;
use Stratadox\Sorting\Sort;
class SomeObject
{
private $name;
private $rating;
public function __construct(string $name, int $rating)
{
$this->name = $name;
$this->rating = $rating;
}
public function name(): string
{
return $this->name;
}
public function rating(): int
{
return $this->rating;
}
}
$objects = [
new SomeObject('Foo', 3),
new SomeObject('Bar', 1),
new SomeObject('Baz', 2),
];
$sorter = new ObjectSorter();
$objects = $sorter->sort($objects, Sort::ascendingBy('rating'));
assert($objects == [
new SomeObject('Bar', 1),
new SomeObject('Baz', 2),
new SomeObject('Foo', 3),
]);
use Stratadox\Sorting\ObjectSorter;
use Stratadox\Sorting\Sort;
class SomeObject
{
private $name;
private $rating;
public function __construct(string $name, int $rating)
{
$this->name = $name;
$this->rating = $rating;
}
public function getName(): string
{
return $this->name;
}
public function getTheRating(): int
{
return $this->rating;
}
}
$objects = [
new SomeObject('Foo', 3),
new SomeObject('Bar', 1),
new SomeObject('Baz', 2),
];
$sorter = new ObjectSorter([
'name' => 'getName',
'rating' => 'getTheRating',
]);
$objects = $sorter->sort($objects, Sort::ascendingBy('rating'));
assert($objects == [
new SomeObject('Bar', 1),
new SomeObject('Baz', 2),
new SomeObject('Foo', 3),
]);