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/ */
// 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