This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/application/classes/lnapp/sort.php

91 lines
2.5 KiB
PHP
Raw Normal View History

2011-07-13 22:59:32 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is used to sort multiple dimension arrays.
*
* @package lnApp
* @subpackage Sort
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
* @uses Style
*/
class lnApp_Sort {
/**
* Sort a multi dimensional array.
*
* @param array Multi demension array passed by reference
* @param string Comma delimited string of sort keys.
* @param boolean Whether to reverse sort.
* @return array Sorted multi demension array.
*/
public static function MAsort(&$data,$sortby,$rev=0) {
// if the array to sort is null or empty, or our sortby is bad
if (! preg_match('/^([a-zA-Z0-9_]+(\([a-zA-Z0-9_,]*\)(->[a-zA-Z0-9])?)?,?)+$/',$sortby) || ! $data)
2011-07-13 22:59:32 +00:00
return;
$code = '$c=0;';
2011-08-31 05:09:03 +00:00
foreach (explode(',',$sortby) as $key) {
$code .= 'if (is_object($a) || is_object($b)) {';
foreach (array('a','b') as $x) {
$code .= 'if (is_array($'.$x.'->'.$key.')) {';
$code .= 'asort($'.$x.'->'.$key.');';
$code .= '$x'.$x.' = array_shift($'.$x.'->'.$key.');';
$code .= '} else';
$code .= '$x'.$x.' = $'.$x.'->'.$key.';';
}
$code .= 'if ($xa != $xb)';
2011-08-31 05:09:03 +00:00
if ($rev)
$code .= 'return ($xa < $xb ? 1 : -1);';
2011-08-31 05:09:03 +00:00
else
$code .= 'return ($xa > $xb ? 1 : -1);';
2011-08-31 05:09:03 +00:00
$code .= '} else {';
2011-08-31 05:09:03 +00:00
foreach (array('a','b') as $x)
$code .= '$'.$x.' = array_change_key_case($'.$x.');';
2011-08-31 05:09:03 +00:00
$key = strtolower($key);
$code .= 'if ((! isset($a[\''.$key.'\'])) && isset($b[\''.$key.'\'])) return 1;';
$code .= 'if (isset($a[\''.$key.'\']) && (! isset($b[\''.$key.'\']))) return -1;';
2011-08-31 05:09:03 +00:00
$code .= 'if ((isset($a[\''.$key.'\'])) && (isset($b[\''.$key.'\']))) {';
foreach (array('a','b') as $x) {
$code .= 'if (is_array($'.$x.'[\''.$key.'\'])) {';
$code .= 'asort($'.$x.'[\''.$key.'\']);';
$code .= '$x'.$x.' = array_shift($'.$x.'[\''.$key.'\']);';
$code .= '} else';
$code .= '$x'.$x.' = $'.$x.'[\''.$key.'\'];';
}
2011-08-31 05:09:03 +00:00
$code .= 'if ($xa != $xb)';
$code .= 'if (is_numeric($xa) && is_numeric($xb)) {';
2011-08-31 05:09:03 +00:00
if ($rev)
$code .= 'return ($xa < $xb ? 1 : -1);';
2011-08-31 05:09:03 +00:00
else
$code .= 'return ($xa > $xb ? 1 : -1);';
2011-08-31 05:09:03 +00:00
$code .= '} else {';
2011-08-31 05:09:03 +00:00
if ($rev)
$code .= 'if (($c = strcasecmp($xb,$xa)) != 0) return $c;';
2011-08-31 05:09:03 +00:00
else
$code .= 'if (($c = strcasecmp($xa,$xb)) != 0) return $c;';
2011-08-31 05:09:03 +00:00
$code .= '}}}';
2011-07-13 22:59:32 +00:00
}
2011-08-31 05:09:03 +00:00
$code .= 'return $c;';
$result = create_function('$a, $b',$code);
2011-08-31 05:09:03 +00:00
uasort($data,$result);
2011-07-13 22:59:32 +00:00
}
}
?>