La forma más rápida de ordenar esta matriz

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'));

¿Alguna idea?

preguntado el 04 de julio de 12 a las 09:07

ksort wont help here as it sorts strings (by key) not arrays -

try to do what I did here: stackoverflow.com/a/11320484/1057429 -

3 Respuestas

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
?>

Probado en 5.2.17

Respondido 04 Jul 12, 10:07

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: 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

interesting, I didnt think sort would be capable if there are multiple arrays thats why I didnt even try it lol thanks - incendio

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.

Vea aqui para mas informacion: http://php.net/manual/en/function.array-multisort.php

Respondido 04 Jul 12, 10:07

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))); 

Respondido 14 ago 13, 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.