Super simple sorting of arrays in PHP
- Sort multi-dimmensional arrays or arrays of objects
- Apply multiple ordering rules (similar to MySQL)
- Case-insensitve sorting (PHP 5.4)
- Sort using a closure or class method
- Sort without modifying array keys
$input = array(
array(
'name' => 'John',
'age' => 30
),
array(
'name' => 'Jack',
'age' => 32
),
array(
'name' => 'James',
'age' => 30
),
);
// Sort by "age" (ascending) first, then "name" (descending)
$output = ArraySorter::create($input)
->addRule('age')
->addRule('name', SORT_DESC)
->sort();
// $output => John, James, Jack
$input = array(
array(
'name' => 'john',
'age' => 30
),
array(
'name' => 'Jack',
'age' => 32
),
array(
'name' => 'James',
'age' => 30
),
);
// Sort by "age" (ascending) then "name" (case insensitive, descending)
$output = ArraySorter::create($input)
->addRule('age')
->addRule('name', SORT_DESC, SORT_STRING | SORT_FLAG_CASE)
->sort();
// $output => jonn, James, Jack
$input = array(
new DateTime('2013-01-01'),
new DateTime('2014-01-01'),
new DateTime('2012-01-01')
);
// Sort by dates by seconds since epoch (ascending)
$output = ArraySorter::create($input)->addRule(function($date) {
// Return the value to be sorted by
return $date->format('U');
})->sort();
// $output => 2012-01-01, 2013-01-01, 2014-01-01
$input = array(
'John' => 31,
'Jack' => 32,
'James' => 30
);
// Sort names by value ASC without modifying keys
$output = ArraySorter::create($input)->sort(TRUE);
// $output => James, John, Jack
- Pass array key to closure function
- Support for pass-by-reference
- Performance improvements
- Case-insensitive sorting for PHP 5.3
- Add Documentation for
SORT_NATURAL
- PHPUnit tests
0.1.1