Dropped Sort class
Moved it off to joshtronic/php-sort
This commit is contained in:
parent
ea28dbbc5e
commit
ce45dc0dbe
2 changed files with 0 additions and 131 deletions
70
src/Sort.php
70
src/Sort.php
|
@ -1,70 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sorting Utility Collection
|
|
||||||
*
|
|
||||||
* Licensed under The MIT License
|
|
||||||
* Redistribution of these files must retain the above copyright notice.
|
|
||||||
*
|
|
||||||
* @copyright Copyright 2007-2014, Josh Sherman
|
|
||||||
* @license http://www.opensource.org/licenses/mit-license.html
|
|
||||||
* @link https://github.com/joshtronic/pickles
|
|
||||||
* @package Pickles
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Pickles;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sort Class
|
|
||||||
*
|
|
||||||
* I got tired of writing separate usort functions to sort by different array
|
|
||||||
* keys in the array, this class solves that.
|
|
||||||
*/
|
|
||||||
class Sort
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Ascending
|
|
||||||
*
|
|
||||||
* Variable to utilize ascending sort
|
|
||||||
*
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
const ASC = 'ASC';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Descending
|
|
||||||
*
|
|
||||||
* Variable to utilize descending sort
|
|
||||||
*
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
const DESC = 'DESC';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sort By
|
|
||||||
*
|
|
||||||
* Sorts an array by the specified column, optionally in either direction.
|
|
||||||
*
|
|
||||||
* @param string $field field to sort by
|
|
||||||
* @param array $array array to sort
|
|
||||||
* @param string $direction optional direction to sort
|
|
||||||
* @retun boolean true because sorting is done by reference
|
|
||||||
*/
|
|
||||||
public static function by($field, &$array, $direction = Sort::ASC)
|
|
||||||
{
|
|
||||||
usort($array, create_function('$a, $b', '
|
|
||||||
$a = $a["' . $field . '"];
|
|
||||||
$b = $b["' . $field . '"];
|
|
||||||
|
|
||||||
if ($a == $b)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ($a ' . ($direction == Sort::DESC ? '>' : '<') .' $b) ? -1 : 1;
|
|
||||||
'));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,61 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
class SortTest extends PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
public function testByNameASC()
|
|
||||||
{
|
|
||||||
$shuffled = [
|
|
||||||
['name' => 'epsilon'],
|
|
||||||
['name' => 'gamma'],
|
|
||||||
['name' => 'alpha'],
|
|
||||||
['name' => 'delta'],
|
|
||||||
['name' => 'beta'],
|
|
||||||
];
|
|
||||||
|
|
||||||
$sorted = [
|
|
||||||
['name' => 'alpha'],
|
|
||||||
['name' => 'beta'],
|
|
||||||
['name' => 'delta'],
|
|
||||||
['name' => 'epsilon'],
|
|
||||||
['name' => 'gamma'],
|
|
||||||
];
|
|
||||||
|
|
||||||
Pickles\Sort::by('name', $shuffled);
|
|
||||||
|
|
||||||
$this->assertEquals($sorted, $shuffled);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testByNameDESC()
|
|
||||||
{
|
|
||||||
$shuffled = [
|
|
||||||
['name' => 'epsilon'],
|
|
||||||
['name' => 'gamma'],
|
|
||||||
['name' => 'alpha'],
|
|
||||||
['name' => 'delta'],
|
|
||||||
['name' => 'beta'],
|
|
||||||
];
|
|
||||||
|
|
||||||
$sorted = [
|
|
||||||
['name' => 'gamma'],
|
|
||||||
['name' => 'epsilon'],
|
|
||||||
['name' => 'delta'],
|
|
||||||
['name' => 'beta'],
|
|
||||||
['name' => 'alpha'],
|
|
||||||
];
|
|
||||||
|
|
||||||
Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC);
|
|
||||||
|
|
||||||
$this->assertEquals($sorted, $shuffled);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testMissingField()
|
|
||||||
{
|
|
||||||
$shuffled = [['foo' => 'bar', 'bar' => 'foo']];
|
|
||||||
$sorted = [['foo' => 'bar', 'bar' => 'foo']];
|
|
||||||
|
|
||||||
Pickles\Sort::by('name', $shuffled);
|
|
||||||
|
|
||||||
$this->assertEquals($sorted, $shuffled);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue