PHP code example of yashuk803 / sorting-array

1. Go to this page and download the library: Download yashuk803/sorting-array 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/ */

    

yashuk803 / sorting-array example snippets




shuk803\Sorting\Sorter;
use Yashuk803\Sorting\Factory;

$factory = new Factory();

$dataset = ['orange', 'charry', 'banana'];

$sorter = new Sorter($factory->createAscendingSort());
print_r($sorter->sort($dataset));
/* 
Array
(
    [0] => orange
    [1] => charry
    [2] => banana
)
*/

$sorter = new Sorter($factory->createDescendingSort());
print_r($sorter->sort($dataset));
/*
Array
(
    [2] => banana
    [1] => charry
    [0] => orange
)
*/

$dataset = [4, 6, 10, 5, 8, 16];

$sorter = new Sorter($factory->createAscendingSort());
print_r($sorter->sort($dataset));
/* 
Array
(
    [5] => 16
    [2] => 10
    [4] => 8
    [1] => 6
    [3] => 5
    [0] => 4
)
*/

$sorter = new Sorter($factory->createDescendingSort());
print_r($sorter->sort($dataset));
/*
 Array
(
    [0] => 4
    [3] => 5
    [1] => 6
    [4] => 8
    [2] => 10
    [5] => 16
)
*/

$sorter = new Sorter($factory->createNullArraySort());
$sorter->setSorterStrategy($factory->createDescendingSort());
print_r($sorter->sort($dataset));
/*
 Array
(
    [0] => 4
    [3] => 5
    [1] => 6
    [4] => 8
    [2] => 10
    [5] => 16
)
*/

$sorter = new Sorter($factory->createDescendingSort());
$sorter->sort($dataset); //null