PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

uasort> <sizeof
Last updated: Fri, 18 Jul 2008

view this page in

sort

(PHP 4, PHP 5)

sort — Ordina un array

Descrizione

bool sort ( array $&array [, int $sort_flags ] )

Questa funzione ordina un array. Gli elementi vengono disposti dal più piccolo al più grande.

Nota: Questa funzione assegna nuove chiavi agli elementi di array . Quindi non si limita a riordinare le chiavi, ma rimuove tutte le chiavi che siano state assegnate.

Restituisce TRUE in caso di successo, FALSE in caso di fallimento.

Example #1 esempio di sort()

<?php

$frutti 
= array("limone""arancia""banana""mela");
sort($frutti);
reset($frutti);
while (list(
$chiave$valore) = each($frutti)) {
    echo 
"frutti[" $chiave "] = " $valore "\n";
}

?>

Questo esempio mostrerà:

frutti[0] = arancia
frutti[1] = banana
frutti[2] = limone
frutti[3] = mela

I frutti sono stati ordinati in ordine alfabetico.

Il secondo parametro opzionale sort_flags può essere usato per modificare il comportamento dell'ordinamento, usando i seguenti valori:

flag d'ordinamento:

  • SORT_REGULAR - compara gli elementi in modo normale
  • SORT_NUMERIC - compara gli elementi numericamente
  • SORT_STRING - compara gli elementi convertiti in stringa

Nota: Il secondo parametro è stato aggiunto in PHP 4.

Vedere anche arsort(), asort(), ksort(), natsort(), natcasesort(), rsort(), usort(), array_multisort() e uksort().



uasort> <sizeof
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
sort
lucazd at gmail dot com
26-Sep-2008 10:04
Please, think twice before creating (and posting) your homemade solution for ordering an array by a field.

PHP already has a solution for this; usort, which is about x40 faster than any other solution.

If creating a callback for each case hurts, you can do something like this:

<?php
/**
 * @author Lucas Dománico
 */
class util {
    static
private $sortfield = null;
    static
private $sortorder = 1;
    static
private function sort_callback(&$a, &$b) {
        if(
$a[self::$sortfield] == $b[self::$sortfield]) return 0;
        return (
$a[self::$sortfield] < $b[self::$sortfield])? -self::$sortorder : self::$sortorder;
    }
    static function
sort(&$v, $field, $asc=true) {
       
self::$sortfield = $field;
       
self::$sortorder = $asc? 1 : -1;
       
usort($v, array('util', 'sort_callback'));
    }
}

# Usage:
for($i=0; $i<1000 ;$i++) {
 
$v[] = array('score'=>rand(1,10000));
}
util::sort($v, 'score');

?>
www at designdetector dot com
09-Sep-2008 04:43
To sort an array of multiple text fields alphabetically you have to make the text lowercase before sorting the array. Otherwise PHP puts acronyms before words. You can see this in my example code. Simply store the original text field at the end of the array line and call it later from there. You can safely ignore the lowercase version which is added to the start of the array line.

<?php
echo '<pre>ORIGINAL DATA:
<br />'
;

$data = array(
'Saturn|7|8|9|0||',
'Hello|0|1|2|3||',
'SFX|5|3|2|4||',
'HP|9|0|5|6||'
);

print_r($data);

sort($data);
reset($data);

echo
'<br />RAW SORT:
<br />'
;

print_r($data);

for (
$c = 0; $c < count($data); $c++) {
    list (
$letter,$g1,$g2,$g3,$g4,$end) = explode ('|', $data[$c]);
   
$lowercase = strtolower($letter);
   
$data2[$c] = array($lowercase,$g1,$g2,$g3,$g4,$letter);
}

sort($data2);
reset($data2);

echo
'<br />LOWERCASE SORT:
<br />'
;

print_r($data2);

echo
'</pre>';
?>
matpatnik at hotmail dot com
24-Jan-2008 12:46
This function will sort entity letters eg:&eacute;

I hope that help someone

function sort_entity($array) {
    $total = count($array);
    for ($i=0;$i<$total;$i++) {
        if ($array[$i]{0} == '&') {
            $array[$i] = $array[$i]{1}.$array[$i];
        } else {
            $array[$i] = $array[$i]{0}.$array[$i];
        }
    }
    sort($array);
   
    for ($i=0;$i<$total;$i++) {
        $array[$i] = substr($array[$i],1);
    }
   
    return $array;
}
jasons at work dot com
07-Jan-2008 09:25
Update to the msort code posted by: alishahnovin

I had a problem with the msort function not being case sensitive. All capital letters A-Z would list in order then all lowercase letters would follow.

the line:
if ($item[$id]<$array[$lowest_id][$id]) {

was changed to:
if (strtolower($item[$id]) < strtolower($array[$lowest_id][$id])) {

<?php
function msort($array, $id="id", $sort_ascending=true) {
       
$temp_array = array();
        while(
count($array)>0) {
           
$lowest_id = 0;
           
$index=0;
            foreach (
$array as $item) {
                if (isset(
$item[$id])) {
                                    if (
$array[$lowest_id][$id]) {
                    if (
strtolower($item[$id]) < strtolower($array[$lowest_id][$id])) {
                       
$lowest_id = $index;
                    }
                    }
                                }
               
$index++;
            }
           
$temp_array[] = $array[$lowest_id];
           
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
        }
                if (
$sort_ascending) {
            return
$temp_array;
                } else {
                    return
array_reverse($temp_array);
                }
    }
?>
david wh thomas at gm at 1l dot c0m
03-Sep-2007 03:02
Here's a variation on the above function to sort arrays with more than one key by an arbitrary key's value.

This function allows sorting of an array of objects too

<?php
   
/**
     * Sorts an array of objects by the value of one of the object properties or array keys
     *
     * @param array $array
     * @param key value $id
     * @param boolean $sort_ascending
     * @param boolean $is_object_array
     * @return array
     */
 
function vsort($array, $id="id", $sort_ascending=true, $is_object_array = false) {
       
$temp_array = array();
        while(
count($array)>0) {
           
$lowest_id = 0;
           
$index=0;
            if(
$is_object_array){
                foreach (
$array as $item) {
                    if (isset(
$item->$id)) {
                                        if (
$array[$lowest_id]->$id) {
                        if (
$item->$id<$array[$lowest_id]->$id) {
                           
$lowest_id = $index;
                        }
                        }
                                    }
                   
$index++;
                }
            }else{
                foreach (
$array as $item) {
                    if (isset(
$item[$id])) {
                        if (
$array[$lowest_id][$id]) {
                        if (
$item[$id]<$array[$lowest_id][$id]) {
                           
$lowest_id = $index;
                        }
                        }
                                    }
                   
$index++;
                }                             
            }
           
$temp_array[] = $array[$lowest_id];
           
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
        }
                if (
$sort_ascending) {
            return
$temp_array;
                } else {
                    return
array_reverse($temp_array);
                }
    }

?>

Sample Usage:

<?php
$nodes
= vsort($nodes,'term_data_weight', false, true);
print
'<pre>'.print_r($nodes,1).'</pre>';
?>
quirk at qns dot se
22-Jul-2007 02:42
Hi, this is my version of sorting an array by field.

From browsing previous versions it pretty much resembles bluej's version. It's way much faster than the versions where the sorting is made "manually" rather than with native php functions, and I wrote it after trying one of those that kept timing out my scripts if I had 10000 posts.

This one preserves numerical keys as well. So if you want to re-index the array after using it with numerical keys just use the array_values on the result. However there are cases where the key actually means something even if it's a number (id etc) so I didn't want to take it for granted that it should be reindexed.

Cheers
Q

<?php
       
function sortArrayByField
       
(
           
$original,
           
$field,
           
$descending = false
       
)
        {
           
$sortArr = array();
           
            foreach (
$original as $key => $value )
            {
               
$sortArr[ $key ] = $value[ $field ];
            }
   
            if (
$descending )
            {
               
arsort( $sortArr );
            }
            else
            {
               
asort( $sortArr );
            }
           
           
$resultArr = array();
            foreach (
$sortArr as $key => $value )
            {
               
$resultArr[ $key ] = $original[ $key ];
            }
       
            return
$resultArr;
        }           
?>
alishahnovin at hotmail dot com
20-Jul-2007 01:16
Someone asked me if the msort I posted below can do a sort by descending... (as it sorts by ascending...smallest to greatest).

It's a simple fix with an extra param, and then an array_reverse...but for the lazy, here you are:

<?php
function msort($array, $id="id", $sort_ascending=true) {
       
$temp_array = array();
        while(
count($array)>0) {
           
$lowest_id = 0;
           
$index=0;
            foreach (
$array as $item) {
                if (isset(
$item[$id])) {
                                    if (
$array[$lowest_id][$id]) {
                    if (
$item[$id]<$array[$lowest_id][$id]) {
                       
$lowest_id = $index;
                    }
                    }
                                }
               
$index++;
            }
           
$temp_array[] = $array[$lowest_id];
           
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
        }
                if (
$sort_ascending) {
            return
$temp_array;
                } else {
                    return
array_reverse($temp_array);
                }
    }
?>

<?php

//oh no, this is not in the ordered by id!!
$data[] = array("item"=>"item 1", "id"=>1);
$data[] = array("item"=>"item 3", "id"=>3);
$data[] = array("item"=>"item 2", "id"=>2);

var_dumpmsort($data, "id", false)  ); //just msort it...greatest to smallest

var_dumpmsort($data, "id")  ); //just msort it...smallest to greatest

/* outputs

array
  0 =>
    array
      'item' => 'item 3' (length=6)
      'id' => 3
  1 =>
    array
      'item' => 'item 2' (length=6)
      'id' => 2
  2 =>
    array
      'item' => 'item 1' (length=6)
      'id' => 1

array
  0 =>
    array
      'item' => 'item 1' (length=6)
      'id' => 1
  1 =>
    array
      'item' => 'item 2' (length=6)
      'id' => 2
  2 =>
    array
      'item' => 'item 3' (length=6)
      'id' => 3

*/

?>
bluej100@gmail
11-Jul-2007 12:03
The sort2d I posted before did nothing by default--that'll teach me to copy-and-paste without thinking. Its sort function has to be associative. I've changed the default to asort. natcasesort does actually work, though.

// $sort used as variable function--can be natcasesort, for example
// WARNING: $sort must be associative
function sort2d( &$arrIn, $index = null, $sort = 'asort') {
  // pseudo-secure--never allow user input into $sort
  if (strpos($sort, 'sort') === false) {$sort = 'asort';}
  $arrTemp = Array();
  $arrOut = Array();

  foreach ( $arrIn as $key=>$value ) {
    $arrTemp[$key] = is_null($index) ? reset($value) : $value[$index];
  }

  $sort($arrTemp);

  foreach ( $arrTemp as $key=>$value ) {
    $arrOut[$key] = $arrIn[$key];
  }

  $arrIn = $arrOut;
}

Also, uasort is probably actually the better solution for most 2d sorting, unless you're sorting by a dozen different indexes.
NBS Studio
04-Jul-2007 10:14
This is my way of sorting files into date modified date order. It worked for me!

$dir='topics';
$ext='php5';
$files=scandir($dir);
foreach($files as $fs){
    if(($fs!='.')&&($fs!='..')){
        $fs1.='¬'.filemtime($dir.'/'.$fs).'#'.$fs;
    }
}
$fs2=split('[¬]',$fs1);
arsort($fs2);
foreach($fs2 as $fs3){
    if(eregi($ext,$fs3)){
        $fs4.='¬'.$fs3;
    }
}
$fs5=split('[#]',$fs4);
foreach($fs5 as $fs6){
    if(eregi($ext,$fs6)){
        $fs7.='¬'.$fs6;
    }
}
$fs8=split('[¬]',$fs7);
foreach($fs8 as $fs9){
    $file_list.=$fs9.'
</br>';
}

print $file_list;
sinan at sinaneldem dot com
24-Jun-2007 05:29
here is little script which will merge arrays, remove duplicates and sort it by alphabetical order:

<?php

$array1
= array('apple', 'banana','pear');
$array2 = array('grape', 'pear','orange');

function
array_unique_merge_sort($array1, $array2){
$array = array_unique(array_merge($array1, $array2));
sort($array);
foreach (
$array as $key => $value) {
   
$new[$key] = $value;
}
return
$new;
}

print_r (array_unique_merge_sort($array1, $array2));

?>

this will print out:

Array ( [0] => apple [1] => banana [2] => grape [3] => orange [4] => pear )
bluej100@gmail
08-Jun-2007 12:05
One more solution for multidimensional sort: variable functions.

<?php
// $sort used as variable function--can be natcasesort, for example
 
function sort2d( &$arrIn, $index = null, $sort = 'sort') {
   
// pseudo-secure--never allow user input into $sort
   
if (strpos($sort, 'sort') === false) {$sort = 'sort';}
   
$arrTemp = Array();
   
$arrOut = Array();

    foreach (
$arrIn as $key=>$value ) {
     
reset($value);
     
$arrTemp[$key] = is_null($index) ? current($value) : $value[$index];
    }

   
$sort($arrTemp);

    foreach (
$arrTemp as $key=>$value ) {
     
$arrOut[$key] = $arrIn[$key];
    }

   
$arrIn = $arrOut;
  }
?>

It appears to me that there are only two algorithms being proposed here (several times each):

1) copy into temp, pass temp to sort function, re-order by temp
2) implement search function in PHP

I'm curious whether anyone's implementation of (2) can beat (1) for speed. Someone have a fast PHP mergesort they can benchmark against this one? Obviously, the fact that the (1) solutions use at least two, possibly three times the memory is a drawback, but I expect that for most of us, speed is significantly more important than memory. Maybe I'll check it myself when I have a minute.
alishahnovin at hotmail dot com
28-May-2007 07:29
Here's my fixed up msort array. What it does is goes through a multidimensional array, and sorts it by the desired key (defaulting to 'id').

So, for example, if you have an array like:

array[0]['value'] = "statement 2"
array[0]['id'] = "2"
array[1]['value'] = "statement 3"
array[1]['id'] = "3"
array[2]['value'] = "statement 1"
array[2]['id'] = "1"

it would rearrange and return the array to be like:

array[0]['value'] = "statement 1"
array[0]['id'] = "1"
array[1]['value'] = "statement 2"
array[1]['id'] = "2"
array[2]['value'] = "statement 3"
array[2]['id'] = "3"

The 'id' index can start at any point, and any array item missing the id index will be added to the end.

<?php
function msort($array, $id="id") {
       
$temp_array = array();
        while(
count($array)>0) {
           
$lowest_id = 0;
           
$index=0;
            foreach (
$array as $item) {
                if (isset(
$item[$id]) && $array[$lowest_id][$id]) {
                    if (
$item[$id]<$array[$lowest_id][$id]) {
                       
$lowest_id = $index;
                    }
                }
               
$index++;
            }
           
$temp_array[] = $array[$lowest_id];
           
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
        }
        return
$temp_array;
    }
?>

Ex:

<?php

//oh no, this is not in the ordered by id!!
$data[] = array("item"=>"item 4");
$data[] = array("item"=>"item 1", "id"=>1);
$data[] = array("item"=>"item 3", "id"=>3);
$data[] = array("item"=>"item 2", "id"=>2);

var_dumpmsort($data)  ); //just msort it!

/* outputs

array
  0 =>
    array
      'item' => 'item 1' (length=6)
      'id' => 1
  1 =>
    array
      'item' => 'item 2' (length=6)
      'id' => 2
  2 =>
    array
      'item' => 'item 3' (length=6)
      'id' => 3
  3 =>
    array
      'item' => 'item 4' (length=6)

*/

?>
alishahnovin at hotmail dot com
26-May-2007 01:11
I had a multidimensional array, which needed to be sorted by one of the keys. This is what I came up with...

<?php
function msort($array, $id="id") {
       
$temp_array = array();
        while(
count($array)>0) {
           
$lowest_id = 0;
           
$index=0;
            foreach (
$array as $item) {
                if (
$item[$id]<$array[$lowest_id][$id]) {
                   
$lowest_id = $index;
                }
               
$index++;
            }
           
$temp_array[] = $array[$lowest_id];
           
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
        }
        return
$temp_array;
    }
?>

Ex:

<?php

//oh no, this is not in the ordered by id!!
$data[] = array("item"=>"item 4", "id"=>4);
$data[] = array("item"=>"item 1", "id"=>1);
$data[] = array("item"=>"item 3", "id"=>3);
$data[] = array("item"=>"item 2", "id"=>2);

var_dumpmsort($data)  ); //just msort it!

/* outputs

array
  0 =>
    array
      'item' => 'item 1' (length=6)
      'id' => 1
  1 =>
    array
      'item' => 'item 2' (length=6)
      'id' => 2
  2 =>
    array
      'item' => 'item 3' (length=6)
      'id' => 3
  3 =>
    array
      'item' => 'item 4' (length=6)
      'id' => 4

*/

?>
(Qube#php@Efnet)
15-May-2007 12:38
How to use an anonymous array to sort any associative array by an arbitrary key (or nested key):

$order = -1; # -1 = Ascending. Use 1 for descending.
$sortby = "['key1']['subkey']";
$mysort = create_function('$a,$b', "\$a1=\$a$sortby;\$b1=\$b$sortby; if (\$a1==\$b1) return 0; else return (\$a1<\$b1) ? $order : 0- $
order;");
uasort($assocarray, $mysort);

You can use this in a recursive function if necessary (which is why I developed it).
Matthew Hood
09-May-2007 04:27
The bubble sort below will sort an array of objects based on any one of the values contained in them.

usage: objectSort($details, 'percent');

function objectSort(&$data, $key)
{
    for ($i = count($data) - 1; $i >= 0; $i--)
    {
      $swapped = false;
      for ($j = 0; $j < $i; $j++)
      {
           if ($data[$j]->$key > $data[$j + 1]->$key)
           {
                $tmp = $data[$j];
                $data[$j] = $data[$j + 1];       
                $data[$j + 1] = $tmp;
                $swapped = true;
           }
      }
      if (!$swapped) return;
    }
}
y5 at yellow5 dot us
15-Mar-2007 03:52
An improvement on the very nice code submitted by alex [at] vkpb [dot] com. This will preserve the keys if the array is numeric:

function SortDataSet($aArray, $sField, $bDescending = false)
{
    $bIsNumeric = IsNumeric($aArray);
    $aKeys = array_keys($aArray);
    $nSize = sizeof($aArray);

    for ($nIndex = 0; $nIndex < $nSize - 1; $nIndex++)
    {
        $nMinIndex = $nIndex;
        $objMinValue = $aArray[$aKeys[$nIndex]][$sField];
        $sKey = $aKeys[$nIndex];

        for ($nSortIndex = $nIndex + 1; $nSortIndex < $nSize; ++$nSortIndex)
        {
            if ($aArray[$aKeys[$nSortIndex]][$sField] < $objMinValue)
            {
                $nMinIndex = $nSortIndex;
                $sKey = $aKeys[$nSortIndex];
                $objMinValue = $aArray[$aKeys[$nSortIndex]][$sField];
            }
        }

        $aKeys[$nMinIndex] = $aKeys[$nIndex];
        $aKeys[$nIndex] = $sKey;
    }

    $aReturn = array();
    for($nSortIndex = 0; $nSortIndex < $nSize; ++$nSortIndex)
    {
        $nIndex = $bDescending ? $nSize - $nSortIndex - 1: $nSortIndex;
        $aReturn[$aKeys[$nIndex]] = $aArray[$aKeys[$nIndex]];
    }

    return $bIsNumeric ? array_values($aReturn) : $aReturn;
}

function IsNumeric($aArray)
{
    $aKeys = array_keys($aArray);
    for ($nIndex = 0; $nIndex < sizeof($aKeys); $nIndex++)
    {
        if (!is_int($aKeys[$nIndex]) || ($aKeys[$nIndex] != $nIndex))
        {
            return false;
        }
    }

    return true;
}
joris at mangrove dot nl
01-Feb-2007 02:40
Commenting on note http://www.php.net/manual/en/function.sort.php#62311 :

Sorting an array of objects will not always yield the results you desire.

As pointed out correctly in the note above, sort() sorts the array by value of the first member variable. However, you can not always assume the order of your member variables! You must take into account your class hierarchy!

By default, PHP places the inherited member variables on top, meaning your first member variable is NOT the first variable in your class definition!
However, if you use code analyzers or a compile cache, things can be very different. E.g., in eAccelerator, the inherited member variables are at the end, meaning you get different sort results with caching on or off.

Conclusion:
Never use sort on arrays with values of a type other than scalar or array.
alex [at] vkpb [dot] com
26-Jan-2007 12:36
Sorting of an array by a method of inserts.

<?

 
       function sortByField($multArray,$sortField,$desc=true){
            $tmpKey='';
            $ResArray=array();

            $maIndex=array_keys($multArray);
            $maSize=count($multArray)-1;

            for($i=0; $i < $maSize ; $i++) {

               $minElement=$i;
               $tempMin=$multArray[$maIndex[$i]][$sortField];
               $tmpKey=$maIndex[$i];

                for($j=$i+1; $j <= $maSize; $j++)
                  if($multArray[$maIndex[$j]][$sortField] < $tempMin ) {
                     $minElement=$j;
                     $tmpKey=$maIndex[$j];
                     $tempMin=$multArray[$maIndex[$j]][$sortField];

                  }
                  $maIndex[$minElement]=$maIndex[$i];
                  $maIndex[$i]=$tmpKey;
            }

           if($desc)
               for($j=0;$j<=$maSize;$j++)
                  $ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]];
           else
              for($j=$maSize;$j>=0;$j--)
                  $ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]];

           return $ResArray;
       }

// make array
$array['aaa']=array("name"=>"vasia","order"=>1);
$array['bbb']=array("name"=>"petia","order"=>2);
$array['ccc']=array("name"=>"kolia","order"=>3);
$array['ddd']=array("name"=>"zenia","order"=>4);

// set sort
$SortOrder=0; // desc by default , 1- asc

var_dump(sortByField($array,'order',$SortOrder));

array
  'ddd' =>
    array
      'name' => 'zenia' (length=5)
      'order' => 4
  'aaa' =>
    array
      'name' => 'vasia' (length=5)
      'order' => 1
  'bbb' =>
    array
      'name' => 'petia' (length=5)
      'order' => 2
  'ccc' =>
    array
      'name' => 'kolia' (length=5)
      'order' => 3

?>
ab454 AT wmchris dot de
28-Nov-2006 10:01
Other Way than below: just use asort() ;)
crudo at pinknet dot cz
28-Nov-2006 02:54
Simple way, how to sort an array without loosing keys:

<?php

$sizes
= $bad = $good = array("d" => "dddd", "a" => "aaaa", "c" => "cccc", "e" => "eeee", "b" => "bbbb");

// original
print_r($sizes);

/*
Array
(
    [d] => dddd
    [a] => aaaa
    [c] => cccc
    [e] => eeee
    [b] => bbbb
)
*/

// bad way
sort($bad);
print_r($bad);

/*
Array
(
    [0] => aaaa
    [1] => bbbb
    [2] => cccc
    [3] => dddd
    [4] => eeee
)
*/

// good way
$good=array_flip($good);
ksort($good);
$good=array_flip($good);

print_r($good);

/*
Array
(
    [a] => aaaa
    [b] => bbbb
    [c] => cccc
    [d] => dddd
    [e] => eeee
)
*/

?>
eran dot liberty at gmail dot com
15-Nov-2006 04:24
/**
 * Will sort an array by the value of the applied lambda function to each element
 * without loosing the keys!
 * @param unknown_type $arr
 * @param unknown_type $func of the form mixvar func(your object)
 */
function sortByFunc(&$arr, $func) {
        $tmpArr = array();
        foreach ($arr as $k => &$e) {
            $tmpArr[] = array('f' => $func($e), 'k' => $k, 'e' =>&$e);
        }
        sort($tmpArr);
        $arr = array();
        foreach($tmpArr as &$fke) {
            $arr[$fke['k']] = &$fke['e'];
        }
}

example:
$arr = array(
    1 => array('name' => 'eran', 'age' => 30),
    2 => array('name' => 'naama', 'age' => 29),
    3 => array('name' => 'a', 'age' => 11),
    4 => array('name' => 'b', 'age' => 51),
    5 => array('name' => 'z', 'age' => 5),
);
foreach($arr as $key => $val) {
    echo "<br> $key => (" . $val['name'] . " ," . $val['age'] . ")";
}
sortByFunc($arr,create_function('$element','return $element["age"];'));
echo "<br> now sorted:";
foreach($arr as $key => $val) {
    echo "<br> $key => (" . $val['name'] . " ," . $val['age'] . ")";
}
output:
1 => (eran ,30)
2 => (naama ,29)
3 => (a ,11)
4 => (b ,51)
5 => (z ,5)
now sorted:
5 => (z ,5)
3 => (a ,11)
2 => (naama ,29)
1 => (eran ,30)
4 => (b ,51)
memzap at gmail dot com
21-Jul-2006 07:31
Here is how you would open a file, and put each line into an array. This sorts by the first field $title field.  The next thing I would like to figure out is how to do this same sort but with the ability to skip the first word of the title. Like if the title has an "a" or "the" it would skip that portion of the sort.

$currentfile = "file.txt";
$fp = fopen( $currentfile, "r" ) or die("Couldn't open $currentfile");
while ( ! feof( $fp ) ) {
    $line[] = fgets( $fp, 1024 );

    foreach ( $line as $newarray ) {
}
    $newline[] = trim($newarray);
    sort($newline);
    list($title1, $titleurl1, $rating1) = split ('\|',
$newline[0]);
    list($title2, $titleurl2, $rating2) = split ('\|',
$newline[1]);
    list($title3, $titleurl3, $rating3) = split ('\|',
$newline[2]);
    list($title4, $titleurl4, $rating4) = split ('\|',
$newline[3]);
g8z at yahoo dot com
15-Jul-2006 01:28
<?php
/**
This sort function allows you to sort an associative array while "sticking" some fields.

$sticky_fields = an array of fields that should not be re-sorted. This is a method of achieving sub-sorts within contiguous groups of records that have common data in some fields.

For example:

$a = array();

$a []= array(
    'name'         => 'Sam',
    'age'         => 23,
    'hire_date'    => '2004-01-01'
);
$a []= array(
    'name'        => 'Sam',
    'age'        => 44,
    'hire_date'    => '2003-03-23'
);
$a []= array(
    'name'        => 'Jenny',
    'age'        => 20,
    'hire_date' => '2000-12-31'
);
$a []= array(
    'name'        => 'Samantha',
    'age'        => 50,
    'hire_date' => '2000-12-14'
);

$sticky_fields = array( 'name' );
print_r( stickysort( $a, 'age', DESC_NUM, $sticky_fields ) );

OUTPUT:

Array
(
    [0] => Array
        (
            [name] => Sam
            [age] => 44
            [hire_date] => 2003-03-23
        )
    [1] => Array
        (
            [name] => Sam
            [age] => 23
            [hire_date] => 2004-01-01
        )
    [2] => Array
        (
            [name] => Jenny
            [age] => 20
            [hire_date] => 2000-12-31
        )
    [3] => Array
        (
            [name] => Samantha
            [age] => 50
            [hire_date] => 2000-12-14
        )
)

Here's why this is the correct output - the "name" field is sticky, so it cannot change its sort order. Thus, the "age" field is only sorted as a sub-sort within records where "name" is identical. Thus, the "Sam" records are reversed, because 44 > 23, but Samantha remains at the bottom, even though her age is 50. This is a way of achieving "sub-sorts" and "sub-sub-sorts" (and so on) within records of identical data for specific fields.

Courtesy of the $5 Script Archive: http://www.tufat.com
**/

define( 'ASC_AZ', 1000 );
define( 'DESC_AZ', 1001 );
define( 'ASC_NUM', 1002 );
define( 'DESC_NUM', 1003 );

function
stickysort( $arr, $field, $sort_type, $sticky_fields = array() ) {
   
$i = 0;
    foreach (
$arr as $value) {
       
$is_contiguous = true;
        if(!empty(
$grouped_arr)) {
           
$last_value = end($grouped_arr[$i]);

            if(!(
$sticky_fields == array())) {
                foreach (
$sticky_fields as $sticky_field) {
                    if (
$value[$sticky_field] <> $last_value[$sticky_field]) {
                       
$is_contiguous = false;
                        break;
                    }
                }
            }
        }
        if (
$is_contiguous)
           
$grouped_arr[$i][] = $value;
        else
           
$grouped_arr[++$i][] = $value;
    }
   
$code = '';
    switch(
$sort_type) {
        case
ASC_AZ:
           
$code .= 'return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);';
            break;
        case
DESC_AZ:
           
$code .= 'return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));';
            break;
        case
ASC_NUM:
           
$code .= 'return ($a["'.$field.'"] - $b["'.$field.'"]);';
            break;
        case
DESC_NUM:
           
$code .= 'return ($b["'.$field.'"] - $a["'.$field.'"]);';
            break;
    }

   
$compare = create_function('$a, $b', $code);

    foreach(
$grouped_arr as $grouped_arr_key=>$grouped_arr_value)
       
usort ( $grouped_arr[$grouped_arr_key], $compare );

   
$arr = array();
    foreach(
$grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        foreach(
$grouped_arr[$grouped_arr_key] as $grouped_arr_arr_key=>$grouped_arr_arr_value)
           
$arr[] = $grouped_arr[$grouped_arr_key][$grouped_arr_arr_key];

    return
$arr;
}
?>
g8z at yahoo dot com
10-Jul-2006 05:58
<?php
/**
This sort function allows you to sort an associative array while "sticking" some fields.

$sticky_fields = an array of fields that should not be re-sorted. This is a method of achieving sub-sorts within contiguous groups of records that have common data in some fields.

Courtesy of the $5 Script Archive: http://www.tufat.com
**/

define( 'ASC_AZ', 1000 );
define( 'DESC_AZ', 1001 );
define( 'ASC_NUM', 1002 );
define( 'DESC_NUM', 1003 );

function
stickysort( $arr, $field, $sort_type, $sticky_fields = array() ) {
   
$i = 0;
    foreach (
$arr as $value) {
       
$is_contiguous = true;
        if(!empty(
$grouped_arr)) {
           
$last_value = end($grouped_arr[$i]);

            if(!(
$sticky_fields == array())) {
                foreach (
$sticky_fields as $sticky_field) {
                    if (
$value[$sticky_field] <> $last_value[$sticky_field]) {
                       
$is_contiguous = false;
                        break;
                    }
                }
            }
        }
        if (
$is_contiguous)
           
$grouped_arr[$i][] = $value;
        else
           
$grouped_arr[++$i][] = $value;
    }
   
$code = '';
    switch(
$sort_type) {
        case
ASC_AZ:
           
$code .= 'return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);';
            break;
        case
DESC_AZ:
           
$code .= 'return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));';
            break;
        case
ASC_NUM:
           
$code .= 'return ($a["'.$field.'"] - $b["'.$field.'"]);';
            break;
        case
DESC_NUM:
           
$code .= 'return ($b["'.$field.'"] - $a["'.$field.'"]);';
            break;
    }

   
$compare = create_function('$a, $b', $code);

    foreach(
$grouped_arr as $grouped_arr_key=>$grouped_arr_value)
       
usort ( $grouped_arr[$grouped_arr_key], $compare );

   
$arr = array();
    foreach(
$grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        foreach(
$grouped_arr[$grouped_arr_key] as $grouped_arr_arr_key=>$grouped_arr_arr_value)
           
$arr[] = $grouped_arr[$grouped_arr_key][$grouped_arr_arr_key];

    return
$arr;
}
?>
g8z at yahoo dot com
10-Jul-2006 05:57
<?php
/**
This sort function allows you to sort an associative array while "sticking" some fields.

$sticky_fields = an array of fields that should not be re-sorted. This is a method of achieving sub-sorts within contiguous groups of records that have common data in some fields.

Courtesy of the $5 Script Archive: http://www.tufat.com
**/

define( 'ASC_AZ', 1000 );
define( 'DESC_AZ', 1001 );
define( 'ASC_NUM', 1002 );
define( 'DESC_NUM', 1003 );

function
stickysort( $arr, $field, $sort_type, $sticky_fields = array() ) {
   
$i = 0;
    foreach (
$arr as $value) {
       
$is_contiguous = true;
        if(!empty(
$grouped_arr)) {
           
$last_value = end($grouped_arr[$i]);

            if(!(
$sticky_fields == array())) {
                foreach (
$sticky_fields as $sticky_field) {
                    if (
$value[$sticky_field] <> $last_value[$sticky_field]) {
                       
$is_contiguous = false;
                        break;
                    }
                }
            }
        }
        if (
$is_contiguous)
           
$grouped_arr[$i][] = $value;
        else
           
$grouped_arr[++$i][] = $value;
    }
   
$code = '';
    switch(
$sort_type) {
        case
ASC_AZ:
           
$code .= 'return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);';
            break;
        case
DESC_AZ:
           
$code .= 'return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));';
            break;
        case
ASC_NUM:
           
$code .= 'return ($a["'.$field.'"] - $b["'.$field.'"]);';
            break;
        case
DESC_NUM:
           
$code .= 'return ($b["'.$field.'"] - $a["'.$field.'"]);';
            break;
    }

   
$compare = create_function('$a, $b', $code);

    foreach(
$grouped_arr as $grouped_arr_key=>$grouped_arr_value)
       
usort ( $grouped_arr[$grouped_arr_key], $compare );

   
$arr = array();
    foreach(
$grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        foreach(
$grouped_arr[$grouped_arr_key] as $grouped_arr_arr_key=>$grouped_arr_arr_value)
           
$arr[] = $grouped_arr[$grouped_arr_key][$grouped_arr_arr_key];

    return
$arr;
}
?>
Emiliyan at ServicesBG dot Com
29-Mar-2006 05:41
#This is a function that will sort an array...
function sort_by($array,  $keyname = null, $sortby) {
   $myarray = $inarray = array();   
   # First store the keyvalues in a seperate array
    foreach ($array as $i => $befree) {
        $myarray[$i] = $array[$i][$keyname];
    }
   # Sort the new array by
    switch ($sortby) {
    case 'asc':
    # Sort an array and maintain index association...
    asort($myarray);
    break;
    case 'arsort':
    # Sort an array in reverse order and maintain index association
    arsort($myarray);
    break;
    case 'natcasesor':
    # Sort an array using a case insensitive "natural order" algorithm
    natcasesort($myarray);
    break;
    }
    # Rebuild the old array
    foreach ( $myarray as $key=> $befree) {
       $inarray[$key] = $array[$key];
    }
    return $inarray;
}
sort_by(); example...
$info = sort_by($myarray, 'name', $use = 'asc');   
print_r($info);