PHP code example of jmf / sort

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

    

jmf / sort example snippets




use Jmf\Sort\AssociativeSorter;

$sorter = new AssociativeSorter();

$unsorted = [
    'foo' => 'def',
    'bar' => 'abc',
    'baz' => 'ghi',
];

$sorted = $sorter->sort($unsorted);



use Jmf\Sort\ByKeySorter;

$sorter = new ByKeySorter();

$unsorted = [
    'def' => 'foo',
    'abc' => 'bar',
    'ghi' => 'baz',
];

$sorted = $sorter->sort($unsorted);



use Jmf\Sort\ByValueSorter;

$sorter = new ByValueSorter();

$unsorted = [
    'def',
    'abc',
    'ghi',
];

$sorted = $sorter->sort($unsorted);



use Jmf\Sort\ByPropertySorter;

$sorter = ByPropertySorter::createDefault();

$unsorted = [
    ['foo' => 'def', 'bar' => 123],
    ['foo' => 'abc', 'bar' => 123],
    ['foo' => 'abc', 'bar' => 23],
    ['foo' => 'ghi', 'bar' => 345],
];

$sorted = $sorter->sort(
    $unsorted,
    [
        PropertyPass::asc(
            '[foo]',
        ),
        PropertyPass::desc(
            '[bar]',
            SORT_NUMERIC,
        ),
    ]
);



use Jmf\Sort\ByPropertySorter;

$sorter = ByPropertySorter::createDefault();

$articles = getArticles(); // Objects from a repository. 

$sorted = $sorter->sort(
    $articles,
    [
        PropertyPass::desc(
            'publicationDate',
        ),
        PropertyPass::asc(
            'author.name',
        ),
        PropertyPass::asc(
            'title',
        ),
    ]
);