PHP code example of pjdietz / complex-sort

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

    

pjdietz / complex-sort example snippets


// Here's some data.
$data = array(
    array(
        'firstName' => 'George',
        'nickname' => 'Geroge Sr.',
        'age' => 62,
        'favoriteColor' => 'orange'
    ),
    array(
        'firstName' => 'George',
        'nickname' => 'GOB',
        'age' => 42,
        'favoriteColor' => 'blue'
    ),
    array(
        'firstName' => 'George',
        'nickname' => 'George Michael',
        'age' => 16,
        'favoriteColor' => 'blue'
    ),
    array(
        'firstName' => 'Buster',
        'nickname' => 'Buster',
        'age' => 32,
        'favoriteColor' => 'violet'
    )
);

// Some comparison functions. These aren't particular *good* ones, as they do
// nothing as far as checking if the array keys exist, but they'll work for
// this demo.

function compareFirstName($a, $b) {
    if ($a['firstName'] == $b['firstName']) {
        return 0;
    }
    return $a['firstName'] > $b['firstName'] ? 1 : -1;
}

function compareAge($a, $b) {
    if ($a['age'] == $b['age']) {
        return 0;
    }
    return $a['age'] > $b['age'] ? 1 : -1;
}

function compareColor($a, $b) {
    if ($a['favoriteColor'] == $b['favoriteColor']) {
        return 0;
    }
    return $a['favoriteColor'] > $b['favoriteColor'] ? 1 : -1;
}

// Arrange some sort functions in a partiular order in an array.
// Pass that array to ComplexSort::makeComplexSortFunction() to combine them.
// Use this complex function with usort() to sort the data.
$sortArrays = array('compareFirstName', 'compareAge', 'compareColor');
$complexFn = ComplexSort::makeComplexSortFunction($sortArrays);
usort($data, $complexFn);

print "By firstName, age, favoriteColor: \n";
foreach ($data as $bluth) {
    print $bluth['nickname'] . "\n";
}

// By firstName, age, favoriteColor:
// Buster
// George Michael
// GOB
// Geroge Sr.

$sortArrays = array('compareColor', 'compareAge');
$complexFn = ComplexSort::makeComplexSortFunction($sortArrays);
usort($data, $complexFn);

print "By favoriteColor, age: \n";
foreach ($data as $bluth) {
    print $bluth['nickname'] . "\n";
}

// By favoriteColor, age:
// George Michael
// GOB
// Geroge Sr.
// Buster


$sort = ComplexSort::makeComplexSortFunction(array(
        function ($a, $b) {
            if ($a->orderTotal == $b->orderTotal) {
                return 0;
            }
            return $a->orderTotal > $b->orderTotal ? 1 : -1;
        },
        function ($a, $b) {
            if ($a->otherField == $b->otherField) {
                return 0;
            }
            return $a->otherField > $b->otherField ? 1 : -1;
        }
    ));
usort($data, $sort);
bash
$ curl -s https://getcomposer.org/installer | php
$ php composer.phar install