Thread: Array Sorting
View Single Post
Old Jan 23rd, 2008, 11:16 AM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Array Sorting

That was what I suspected but I wasn't quite sure how to implement. I actually find a great tutorial http://www.the-art-of-web.com/php/sortarray/ which is exactly like what you're stating, I came up with the following:
PHP Syntax (Toggle Plain Text)
  1. function customSort($a, $b){
  2. global $sortBy;
  3.  
  4. $retval = 0;
  5. reset($sortBy);
  6.  
  7. do{
  8. $retval = strnatcmp($a[current($sortBy)], $b[current($sortBy)]);
  9. }while(!$retval && next($sortBy) !== false);
  10.  
  11. return $retval;
  12. }
  13. ?>
Where the variable $sortBy is actually an array containing the order of keys in which a user would wish to perform the sorting so in the example I gave something like $sortBy = array('lastName', 'firstName'); What I'm trying to figure out now is how I could do it recursively assuming there were repeating keys within a nested array.
__________________
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