Sort class!!~!
This commit is contained in:
parent
dd6ba92e28
commit
6ac911b78a
1 changed files with 72 additions and 0 deletions
72
classes/Sort.php
Normal file
72
classes/Sort.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Sorting Utility Collection
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <pickles@joshtronic.com>
|
||||
* @copyright Copyright 2007-2013, Josh Sherman
|
||||
* @license http://www.opensource.org/licenses/mit-license.html
|
||||
* @package PICKLES
|
||||
* @link https://github.com/joshtronic/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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue