PHP code example of lemurro / lib-sorter

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

    

lemurro / lib-sorter example snippets


$sorter->run(&$array, $key_name, $order_type, $save_keys = true);

use Lemurro\Lib\Sorter\Sorter;

$sorter = new Sorter();

$array = [
    'z' => [
        'field1' => 'A',
        'field2' => 'orange',
    ],
    'x' => [
        'field1' => 'C',
        'field2' => 'apple',
    ],
    'c' => [
        'field1' => 'B',
        'field2' => 'peach',
    ],
];

$sorter->run($array, 'field1', 'asc');

var_dump($array);
/*
[
    'z' => [
        'field1' => 'A'
        'field2' => 'orange'
    ]
    'c' => [
        'field1' => 'B'
        'field2' => 'peach'
    ]
    'x' => [
        'field1' => 'C'
        'field2' => 'apple'
    ]
]
*/

use Lemurro\Lib\Sorter\Sorter;

$sorter = new Sorter();

$array = [
    'z' => [
        'field1' => 'A',
        'field2' => 'orange',
    ],
    'x' => [
        'field1' => 'C',
        'field2' => 'apple',
    ],
    'c' => [
        'field1' => 'B',
        'field2' => 'peach',
    ],
];

$sorter->run($array, 'field2', 'desc');

var_dump($array);
/*
[
    'x' => [
        'field1' => 'C'
        'field2' => 'apple'
    ]
    'c' => [
        'field1' => 'B'
        'field2' => 'peach'
    ]
    'z' => [
        'field1' => 'A'
        'field2' => 'orange'
    ]
]
*/

use Lemurro\Lib\Sorter\Sorter;

$sorter = new Sorter();

$array = [
    'z' => [
        'field1' => 'A',
        'field2' => 'orange',
    ],
    'x' => [
        'field1' => 'C',
        'field2' => 'apple',
    ],
    'c' => [
        'field1' => 'B',
        'field2' => 'peach',
    ],
];

$sorter->run($array, 'field1', 'asc', false);

var_dump($array);
/*
[
    0 => [
        'field1' => 'A'
        'field2' => 'orange'
    ]
    1 => [
        'field1' => 'B'
        'field2' => 'peach'
    ]
    2 => [
        'field1' => 'C'
        'field2' => 'apple'
    ]
]
*/