Last active
June 27, 2024 00:54
-
-
Save oplanre/3fb01f7fef6f37643c49f647c0b78009 to your computer and use it in GitHub Desktop.
Light abstraction layer for sorting arrays in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Sorter { | |
public function __construct(private array $array) {} | |
/** | |
* Sort the array by a specific key in ascending or descending order. | |
* | |
* @param string $key The key to sort by. | |
* @param string $order The order direction, 'asc' for ascending or 'desc' for descending. Default is 'asc'. | |
* @return self | |
* | |
* @example | |
* $sorted = $sorter->sortBy('name', 'asc')->getArray(); | |
*/ | |
public function sortBy(string $key, string $order = 'asc'): self { | |
usort($this->array, fn($a, $b) => $this->compare($a[$key], $b[$key], $order)); | |
return $this; | |
} | |
/** | |
* Sort the array by multiple keys. | |
* | |
* @param array $keys An associative array of keys and order directions, e.g., ['name' => 'asc', 'age' => 'desc']. | |
* @return self | |
* | |
* @example | |
* $sorted = $sorter->sortByMultiple(['name' => 'asc', 'age' => 'desc'])->getArray(); | |
*/ | |
public function sortByMultiple(array $keys): self { | |
usort($this->array, function($a, $b) use ($keys) { | |
foreach ($keys as $key => $order) { | |
$result = $this->compare($a[$key], $b[$key], $order); | |
if ($result !== 0) { | |
return $result; | |
} | |
} | |
return 0; | |
}); | |
return $this; | |
} | |
/** | |
* Sort the array using a custom comparison function. | |
* | |
* @param callable $comparisonFunction A user-defined comparison function. | |
* @return self | |
* | |
* @example | |
* $sorted = $sorter->sortByCustom(function($a, $b) { | |
* return strcmp($a['name'], $b['name']); | |
* })->getArray(); | |
*/ | |
public function sortByCustom(callable $comparisonFunction): self { | |
usort($this->array, $comparisonFunction); | |
return $this; | |
} | |
/** | |
* Get the sorted array. | |
* | |
* @return array The sorted array. | |
*/ | |
public function getArray(): array { | |
return $this->array; | |
} | |
/** | |
* Compare two values based on the order. | |
* | |
* @param mixed $a The first value to compare. | |
* @param mixed $b The second value to compare. | |
* @param string $order The order direction, 'asc' for ascending or 'desc' for descending. | |
* @return int The comparison result. | |
*/ | |
private function compare($a, $b, string $order): int { | |
$result = $a <=> $b; | |
return ($order === 'asc') ? $result : -$result; | |
} | |
} | |
//Example Usage | |
// Sample array of associative arrays | |
$people = [ | |
['name' => 'Alice', 'age' => 25], | |
['name' => 'Bob', 'age' => 30], | |
['name' => 'Charlie', 'age' => 20], | |
['name' => 'Dave', 'age' => 25], | |
]; | |
// Create a Sorter instance | |
$sorter = new Sorter($people); | |
// Sort by name in ascending order | |
$sortedByNameAsc = $sorter->sortBy('name', 'asc')->getArray(); | |
// Sort by age in descending order | |
$sortedByAgeDesc = $sorter->sortBy('age', 'desc')->getArray(); | |
// Sort by name in ascending order and then by age in descending order | |
$sortedByMultiple = $sorter->sortByMultiple(['name' => 'asc', 'age' => 'desc'])->getArray(); | |
// Sort using a custom comparison function | |
$sortedByCustom = $sorter->sortByCustom(function($a, $b) { | |
return strcmp($a['name'], $b['name']); | |
})->getArray(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment