1. Go to this page and download the library: Download phpextra/sorter 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/ */
phpextra / sorter example snippets
use PHPExtra\Sorter\Sorter;
$data = array('ccc', 'aaa', 'bbb');
$sorter = new Sorter();
$data = $sorter->sort($data);
print_r($data); // prints array('aaa', 'bbb', 'ccc');
use PHPExtra\Sorter\Sorter;
use PHPExtra\Sorter\Strategy\SimpleSortStrategy;
use PHPExtra\Sorter\Comparator\UnicodeCIComparator;
$strategy = new SimpleSortStrategy();
$strategy->setComparator(new UnicodeCIComparator('pl_PL'));
$sorter = new Sorter($strategy);
$sorter->sort(...);
use PHPExtra\Sorter\Sorter;
use PHPExtra\Sorter\Strategy\SimpleSortStrategy;
use PHPExtra\Sorter\Comparator\UnicodeCIComparator;
$array = array(0 => 'a', 1 => 'c', 2 => 'b');
$strategy = new SimpleSortStrategy();
$strategy->setMaintainKeyAssociation(true);
$sorter = new Sorter($strategy);
$sorter->sort($array);
print_r($array); // prints array(0 => 'a', 2 => 'b', 1 => 'c')
use PHPExtra\Sorter\Sorter;
use PHPExtra\Sorter\Strategy\ComplexSortStrategy;
$data = array(
(object)array('name' => 'Ann', 'position' => '3', 'rating' => '3'),
(object)array('name' => 'Ann', 'position' => '2', 'rating' => '2'),
(object)array('name' => 'Ann', 'position' => '2', 'rating' => '1'),
(object)array('name' => 'Betty', 'position' => '1', 'rating' => '2'),
);
$strategy = new ComplexSortStrategy();
$strategy
->setSortOrder(Sorter::ASC)
->sortBy('position') // sort by position
->sortBy('name') // sort by name if position is equal
->sortBy(function($object){return $object->rating}) // sort by rating if name is equal
;
$sorter = new PHPExtra\Sorter\Sorter();
$data = $sorter->setStrategy($strategy)->sort($data);
print_r($data);
// prints:
//
// Array
// (
// [0] => stdClass Object
// (
// [name] => Betty
// [position] => 1
// [rating] => 2
// )
//
// [1] => stdClass Object
// (
// [name] => Ann
// [position] => 2
// [rating] => 1
// )
//
// [2] => stdClass Object
// (
// [name] => Ann
// [position] => 2
// [rating] => 2
// )
//
// [3] => stdClass Object
// (
// [name] => Ann
// [position] => 3
// [rating] => 3
// )
// )
$strategy
->setSortOrder(Sorter::ASC)
->sortBy('position')
->sortBy('name', Sorter::DESC, new MyOwnPropertyComparator())
->sortBy('rating')
;
// or set your own comparator
$strategy->setComparator(new MyOwnPropertyComparator());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.