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.
lnapp/classes/lnApp/Sort.php

93 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2013-04-22 05:50:28 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is used to sort multiple dimension arrays.
*
* @package lnApp
2013-10-08 23:24:11 +00:00
* @category Helpers
2013-04-22 05:50:28 +00:00
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
* @uses Style
*/
abstract 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,array $sortby=array(),$rev=0) {
2013-04-25 00:22:36 +00:00
// if the array to sort is null
if (! $data OR ! $sortby)
2013-04-22 05:50:28 +00:00
return;
2013-04-25 00:22:36 +00:00
// 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)
# return;
2013-04-22 05:50:28 +00:00
$code = '$c=0;';
foreach ($sortby as $key) {
2013-04-22 05:50:28 +00:00
$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.');';
2013-11-14 12:22:26 +00:00
$code .= '} else ';
2013-04-22 05:50:28 +00:00
$code .= '$x'.$x.' = $'.$x.'->'.$key.';';
}
$code .= 'if ($xa != $xb)';
if ($rev)
$code .= 'return ($xa < $xb ? 1 : -1);';
else
$code .= 'return ($xa > $xb ? 1 : -1);';
$code .= '} else {';
foreach (array('a','b') as $x)
$code .= '$'.$x.' = array_change_key_case($'.$x.');';
$key = strtolower($key);
$code .= 'if ((! isset($a[\''.$key.'\'])) && isset($b[\''.$key.'\'])) return 1;';
$code .= 'if (isset($a[\''.$key.'\']) && (! isset($b[\''.$key.'\']))) return -1;';
$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.'\']);';
2013-11-14 12:22:26 +00:00
$code .= '} else ';
2013-04-22 05:50:28 +00:00
$code .= '$x'.$x.' = $'.$x.'[\''.$key.'\'];';
}
$code .= 'if ($xa != $xb)';
$code .= 'if (is_numeric($xa) && is_numeric($xb)) {';
if ($rev)
$code .= 'return ($xa < $xb ? 1 : -1);';
else
$code .= 'return ($xa > $xb ? 1 : -1);';
$code .= '} else {';
if ($rev)
$code .= 'if (($c = strcasecmp($xb,$xa)) != 0) return $c;';
else
$code .= 'if (($c = strcasecmp($xa,$xb)) != 0) return $c;';
$code .= '}}}';
}
$code .= 'return $c;';
$result = create_function('$a, $b',$code);
uasort($data,$result);
}
}
?>