Thread: Array Sorting
View Single Post
Old Jan 24th, 2008, 9:30 AM   #9
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Array Sorting

Yeah... I know... recursion... T_T The bane of my existence. I've written about one recursive function in my life that I've understood lol. Not sure what you mean by your last sentence though. In any case I modified the sorting function to the following:
php Syntax (Toggle Plain Text)
  1. function makeSortFunction($sortBy, $reverse = false){
  2. $code = '$sortBy = array(';
  3. foreach($sortBy as $value){
  4. $code .= "'" . $value . "', ";
  5. }
  6. $code = substr($code, 0, -2);
  7. $code .= ');';
  8. if($reverse) $code .= '$reverse = -1;';
  9. else $code .= '$reverse = 1;';
  10. $code .= '$retval = 0;';
  11. $code .= 'foreach($sortBy as $values){';
  12. $code .= '$retval = strnatcmp($a[$values], $b[$values]);';
  13. $code .= '$retval *= $reverse;';
  14. $code .= 'if($retval) break;';
  15. $code .= '}';
  16. $code .= 'return $retval;';
  17. return create_function('$a, $b', $code);
  18. }
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote