0

I am trying to figure out the quickest way to sort this array (i.e. performance wise).

I want to sort this alphabetically by the first key in each array...

$results = array(array('foo', '=', '1'), array('dog', '!=', '5'), array('apple', '<', '4'));

After the sort shoud look like...

$results = array(array('apple', '<', '4'), array('dog', '!=', '5'), array('foo', '=', '1'));

Any ideas?

fire
  • 21,383
  • 17
  • 79
  • 114

3 Answers3

0

The sort() function just worked as you want to :

<?php
$results = array(array('foo', '=', '1'), array('dog', '!=', '5'), array('apple', '<', '4'));
sort($results);
//$results is now what you want
?>

Tested on 5.2.17

niconoe
  • 1,191
  • 1
  • 11
  • 25
  • Sure! the PHP sort() function has not been modified since PHP 5, and using Quicksort, which is the best known algorithm to sort. Also, if your first key is not a string and you want to compare it as a string, you can use sort($array, SORT_STRING). You can read this for more information: http://php.net/manual/en/function.sort.php . I'm not sure but sort can know if all elements are array and, if they are, they're sorting by first, then second, then third, then...then last element... – niconoe Jul 04 '12 at 09:24
  • interesting, I didnt think `sort` would be capable if there are multiple arrays thats why I didnt even try it lol thanks – fire Jul 04 '12 at 11:08
0

You may find that array_multisort is what you need:

<?php
        $sort_array = array();

        foreach($results as $key => $sub_array){
                $sort_array[$key] = $sub_array[0];//fill the sort array with the values you want to sort on
        }

        array_multisort($sort_array, $results);
?>

The array_multisort function sorts the first array given and then uses the key order to sort the second array. You can probably create the sort array in the same loop that creates your initial result set.

See here for more info: http://php.net/manual/en/function.array-multisort.php

Jon Hazan
  • 101
  • 2
0

usort and multisort are relatively slow - which becomes a pain >100 K records. However PHPs strength are associative arrays. Just build an index based on that core function and apply a quick ksort based on this. Here is what I mean:

function array_index($dataarr,sortingkeyfields){ 
  $indexarr=array(); 
  sortingkeyfieldsnr=count(sortingkeyfields);
  foreach($dataarr as $key=>$valarr) { 
    $currentindex=''; 
    foreach(sortingkeyfieldspos=0;sortingkeyfieldspos<sortingkeyfieldsnr;sortingkeyfieldspos++) $currentindex.= $valarr[sortingkeyfieldspos].'_'; 
    $indexarr[$currentindex]=$key; 
  } 
  return $indexarr; 
} 
function array_sortbyindex(&$dataarr,$indexarr,$issortindexbykey=true){ 
  // assumes complete index!: each item of indexarr must correlate a key of dataarr 
  $resultarr=array(); 
  if($issortindexbykey) ksort($indexarr); 
  foreach($indexarr as $datakey) $resultarr[$datakey]= $dataarr[$datakey]; 
  return $resultarr; 
} 

sortingkeyfields=array(0,2);
print_r(array_sortbyindex($dataarr, array_index($YOURARRAY,sortingkeyfields))); 
Quicker
  • 1,247
  • 8
  • 16