Dropped Sort class

Moved it off to joshtronic/php-sort
This commit is contained in:
Josh Sherman 2014-10-17 07:18:47 -04:00
parent ea28dbbc5e
commit ce45dc0dbe
2 changed files with 0 additions and 131 deletions

View file

@ -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;
}
}

View file

@ -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);
}
}