Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: sort - Manual
[go: Go Back, main page]

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

search for in the

uasort" width="11" height="7"/> <sizeof
Last updated: Thu, 31 May 2007

view this page in

sort

(PHP 4, PHP 5)

sort — 配列をソートする

説明

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

この関数は配列をソートします。この関数が正常に終了すると、 各要素は低位から高位へ並べ替えられます。

注意: この関数は、 array パラメータの要素に対して新しいキーを割り当てます。 その際、単純にキーを並べ替える代わりに、 すでに割り当てられている既存のキーを削除してしまいます。

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例 319. sort() の例

<?php

$fruits
= array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach (
$fruits as $key => $val) {
    echo
"fruits[" . $key . "] = " . $val . "\n";
}

?>

上の例の出力は以下となります。


fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

    

fruits はアルファベットの昇順にソートされました。

オプションの 2 番目のパラメータ sort_flags は、以下の値によりソートの動作を修正するために使用することが可能です。

ソート型のフラグ:

  • SORT_REGULAR - 通常通りに項目を比較 (型は変更しません)
  • SORT_NUMERIC - 数値的に項目を比較
  • SORT_STRING - 文字列として項目を比較
  • SORT_LOCALE_STRING - は、カレントのロケールに に基づき比較を行います。PHP 4.4.0 と PHP 5.0.2で追加されました。 PHP 6 より前のバージョンではシステムロケールを使用します。これは setlocale() を使用して変更可能です。 PHP 6 以降では、i18n_loc_set_default() 関数を 使用する必要があります。

注意: 2 番目の引数は PHP 4 で追加されました。

警告

複数の型が混在する配列をソートする場合には、注意してください。 sort() が予測不可能な結果を出力することがあります。

arsort()asort()ksort()krsort()natsort()natcasesort()rsort()usort()array_multisort() および uksort() も参照ください。



uasort" width="11" height="7"/> <sizeof
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
sort
quirk at qns dot se
22-Jul-2007 09: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 08: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 07: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
05-Jul-2007 05: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 12: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 07: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
29-May-2007 02: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 08: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 07: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 11: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 11: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 10: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 08: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
29-Nov-2006 06:01
Other Way than below: just use asort() ;)
crudo at pinknet dot cz
28-Nov-2006 10: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
16-Nov-2006 12: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
22-Jul-2006 02: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 08: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
11-Jul-2006 12: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
11-Jul-2006 12: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 12: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);
ludvig dot ericson at gmail dot com
26-Feb-2006 02:48
A tip for those who like "raul at jimi dot com dot mx" need to preserve keys after changing stuff in the middle of an array:
array_values.

Example:
<?php
$array
= array(1, 2, 5, 9, 3);
unset(
$array[3]); // Remove index 3, which is 9.
$array = array_values($array);
?>

Hint: array_values can be fine for removing keys and reindex them by number instead, too (applies to functions like posix_pwgetuid which returns an associative array, unlike C and others, call array_values on it, and it'll be the same format IIRC.)
jesper at snt dot utwente dot nl
25-Feb-2006 06:26
If you sort an array of objects, the first variable in the object will be used for sorting:

<?php
class foo
{
  var
$value; //First variable: Used for sorting
 
var $id;

  function
foo($i, $v)
  {
    
$this->id = $i;
    
$this->value = $v;
  }

}

for (
$i = 0; $i < 10; $i++)
{
 
$bar[] = new foo($i,rand(1,10));
}

// This will sort on value
sort($bar);
print_r($bar);
?>

Compare the piece of code above with the following:

<?php
class foo
{
  var
$id; //First variable: Used for sorting
 
var $value;

  function
foo($i, $v)
  {
    
$this->id = $i;
    
$this->value = $v;
  }

}

for (
$i = 0; $i = 10; $i++)
{
 
$bar[] = new foo($i,rand(1,10));
}

// This will sort on id
sort($bar);
print_r($bar);
?>

As you can see the location of declaration of the variables matter!
If you want to sort on both or on a combination of variables, use ksort()
jerome a-t+ yamafoto d*o*t com
23-Feb-2006 02:39
when sorting an array, beware of variable type from elements you put in this array

Example:

$a = 2; // $a is an integer
$b = 'item';
$arr = array($a, $b);
sort($arr);

print_r($arr);

this will output:
$arr[0] = 'item';
$arr[1] = 2;

$a = '2'; // $a is a string
$b = 'item';
$arr = array($a, $b);
sort($arr);

print_r($arr);

this will output:
$arr[0] = '2';
$arr[1] = 'item'

to avoid this problem use:

sort($arr, SORT_STRING)
nm at thenoodleman dot com
31-Jan-2006 10:18
Faster, more effective function:

array_sort (array, ['asc'/'desc'])

Second parameter specifies whether to order ascending or descending. Default is ascending.

function array_sort($array, $type='asc'){
    $result=array();
    foreach($array as $var => $val){
        $set=false;
        foreach($result as $var2 => $val2){
            if($set==false){
                if($val>$val2 && $type=='desc' || $val<$val2 && $type=='asc'){
                    $temp=array();
                    foreach($result as $var3 => $val3){
                        if($var3==$var2) $set=true;
                        if($set){
                            $temp[$var3]=$val3;
                            unset($result[$var3]);
                        }
                    }
                    $result[$var]=$val;   
                    foreach($temp as $var3 => $val3){
                        $result[$var3]=$val3;
                    }
                }
            }
        }
        if(!$set){
            $result[$var]=$val;
        }
    }
    return $result;
}

Works for ordering by integers or strings, no need to specify which.

Example:

$array=array('a' => 50, 'b' => 25, 'c' => 75);
print_r(array_sort($array));

Returns:
Array
(
[b] => 25
[a] => 50
[c] => 75
)
james at miicro dot net
25-Jan-2006 03:26
Further to john dot dutcher at highmark dot com's comments - padding the name could cause a problem if you get abnormally long names, it might be better to rebuild the array thus:

Array (
[0] => Array ( [sortname_01] => Dutcher [sortname_02] => F [sortname_03] => John [name] => Dutcher, John F )
[1] => Array ( [sortname_01] => Dutch [sortname_02] => A [sortname_03] => Roger [name] => Dutch, Roger A )
[2] => Array ( [sortname_01] => Dut [sortname_02] => H [sortname_03] => Maurice [name] => Dut, Maurice H )
[3] => Array ( [sortname_01] => Dut [sortname_02] => S [sortname_03] => Mildred [name] => Dut, Mildred S )
)

which should give:

Array (
[0] => Array ( [sortname_01] => Dut [sortname_02] => H [sortname_03] => Maurice [name] => Dut, Maurice H )
[1] => Array ( [sortname_01] => Dut [sortname_02] => S [sortname_03] => Mildred [name] => Dut, Mildred S )
[2] => Array ( [sortname_01] => Dutch [sortname_02] => A [sortname_03] => Roger [name] => Dutch, Roger A )
[3] => Array ( [sortname_01] => Dutcher [sortname_02] => F [sortname_03] => John [name] => Dutcher, John F )
)
john dot dutcher at highmark dot com
11-Jan-2006 09:58
Regarding the array sorting of names by 'whatever' ....

That post addresses a nearly identical issue with sorting that I also have.

I just wanted to inquire though.........wouldn't the building of the 'sortname'  value require that the last name and first name be padded to their full size as they are placed into the
'sortname' field in order to get proper sorting/alignment ?

Otherwise would one have .......

    dutcherjohnf                   (Dutcher, John F)
    dutchrogera                   (Dutch, Roger A)
    dutmauriceh                   (Dut, Maurice H)

Instead of:

    dut           maurice         h    
    dutch         roger          a
    dutcher      john            f

John D.
raul at jimi dot com dot mx
02-Dec-2005 02:50
I had an array like this:
$arr=array (1,4,3,6,5);

which returns this:
$arr[0]=1
$arr[1]=4
$arr[2]=3
$arr[3]=6
$arr[4]=5

But lets say i remove [2] which is number 3, i get:

$arr[0]=1
$arr[1]=4
$arr[3]=6
$arr[4]=5

And i want to reindex without doing a sort because i dont want to lose the order of the numbers (like a pop in a stack but in the middle of the list), i do this:

$arr=array_chunk($arr,count($arr));
$arr=$arr[0];

the result is:

$arr[0]=1
$arr[1]=4
$arr[2]=6
$arr[3]=5

This can be applied mostly for tree sorting, when you only have the id and the parent values of the node, and you want to have N levels.
phillip dot metzger at gmail dot com
10-Nov-2005 02:36
For a case insensitive sort use this array_multisort().

<?php
$array_var
= Array("Cheery", "zap", "Banana", "apple", "Zing");

array_multisort((strtolower($array_var)), SORT_ASC, SORT_STRING, $array_var);
?>

Result order:
apple
Banana
Cheery
zap
Zing
james at miicro dot net
19-Jul-2005 06:49
It's useful to know that if you're using this function on a multidimensional array, php will sort the first key, then the second and so on. This is similar to being able to use SQL to order by field1, field2 etc.

So:

Array (
[0] => Array ( [category] => work [name] => Smith )
[1] => Array ( [category] => play [name] => Johnson )
[2] => Array ( [category] => work [name] => Berger )
)

will become:

Array (
[0] => Array ( [category] => play [name] => Johnson )
[1] => Array ( [category] => work [name] => Berger )
[2] => Array ( [category] => work [name] => Smith )
)

Hope it helps someone.
whatever at toposphere dot com
03-Jul-2005 05:12
The usort solution below still has problems when sorting strings of different lengths, just like the other flavors of sort that I've tried.

Here's a solution that I came up with -- it's kinda ugly, and it might not be practical for large arrays, but it's works great for me.

I'm getting some people's names out of mySQL and placing them in an array like this: "$lastname, $firstname $middlename". Because some people don't have middle names, "sort" screws up the alphabetization. So what I've done is this:

$index = 0;
while ($rows = mysql_fetch_array($sqlResult)) {
    extract($rows);
    $my_array[$index]['sortname'] = strtolower(preg_replace("/[^a-zA-Z]/", "", "$lastname$firstname$middlename"));
    $my_array[$index]['name'] = trim("$lastname, $firstname $middlename");
    $index++;
}
sort($my_array); // now in perfect alpha order

My "hack" is in the fourth line -- I take the entire name, last name first, and use preg_replace() to take out all non-letter characters, and then used strtolower() to make it all lowercase to ensure all letters would be treated equally by PHP. This goes into the ['sortname'] key within the array.

(The ['sortname'] value has to come before ['name'], or else the array will be sorted by ['name'] instead.)

This gives me:

Array
(
   [0] => Array ( [sortname] => shmojoe [name] => Shmo, Joe )
   [1] => Array ( [sortname] => shmojoem [name] => Shmo, Joe M )
)
... and so on

So the array gets sorted by ['sortname'], which never needs to be displayed, and the associated ['name'] can be displayed in perfect alphabetical order, whether there is one name, two names or three. And because ['sortname'] is letters only, it works great for hyphenated last names or ones with apostrophes, like Smith-Jones or O'Donnell.

I feel that the dual preg_replace() and strtolower() may not be the best theoretical solution, and I don't know how it would run on a really large array, but as I said, it's a great solution for my little site.
dwatson at planandgrow dot com
19-Mar-2005 10:55
Here is a recursive use of sort for multi-dim arrays:

<?php

/*Mulsort function recursively sorts a  multi-dimensional, numeric array in place,
regardless of how many dimensions it has.  The array can be ragged -- not a matrix. 
*/

function mulsort(&$a)
{
   
sort($a);
   
$c = count($a);
    for(
$i = 0; $i < $c; $i++)
        if (
is_array($a[$i]))
           
mulsort($a[$i]);
}

//The following array is just used to demo the mulsort function.
$array_demo = array ( array ( array (333,1,9,8,7,6,5), array (array(1, 'x', 22), 99, 88 ), -10 ),
        array ( array (
11, 12, 'a', 'b', 'c'), array (8, 1, 2,-5) ) );

echo
"Array before sorting:<br /><pre>";
var_dump($array_demo);
echo
"</pre></ br></ br>";

mulsort($array_demo);

echo
"Array after sorting:<br /><pre>";
var_dump($array_demo);
echo
"</pre>";

?>
timc at hlyw dot com
18-Feb-2005 09:04
I dig the multi_sort function(s) from above.  But, they don't work for hash arrays.  I added a keys variable to keep track of the key value as the array gets sorted.  Feed back welcome.

<?php
function array_qsort (&$array, $column=0, $order=SORT_ASC, $first=0, $last= -2)
{
 
// $array  - the array to be sorted
  // $column - index (column) on which to sort
  //          can be a string if using an associative array
  // $order  - SORT_ASC (default) for ascending or SORT_DESC for descending
  // $first  - start index (row) for partial array sort
  // $last  - stop  index (row) for partial array sort
  // $keys  - array of key values for hash array sort
 
 
$keys = array_keys($array);
  if(
$last == -2) $last = count($array) - 1;
  if(
$last > $first) {
  
$alpha = $first;
  
$omega = $last;
  
$key_alpha = $keys[$alpha];
  
$key_omega = $keys[$omega];
  
$guess = $array[$key_alpha][$column];
   while(
$omega >= $alpha) {
     if(
$order == SORT_ASC) {
       while(
$array[$key_alpha][$column] < $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
       while(
$array[$key_omega][$column] > $guess) {$omega--; $key_omega = $keys[$omega]; }
     } else {
       while(
$array[$key_alpha][$column] > $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
       while(
$array[$key_omega][$column] < $guess) {$omega--; $key_omega = $keys[$omega]; }
     }
     if(
$alpha > $omega) break;
    
$temporary = $array[$key_alpha];
    
$array[$key_alpha] = $array[$key_omega]; $alpha++;
    
$key_alpha = $keys[$alpha];
    
$array[$key_omega] = $temporary; $omega--;
    
$key_omega = $keys[$omega];
   }
  
array_qsort ($array, $column, $order, $first, $omega);
  
array_qsort ($array, $column, $order, $alpha, $last);
  }
}
?>
anthony at ectrolinux dot com
09-Sep-2004 08:39
In a brief addition to the previous poster's message, the ascending sorting order used by PHP directly corresponds to ISO-8859-1 (ASCII). Therefore the character \48 (numeral 0) would be placed before the character \82 (R), which would be placed before the character \110 (n), and so forth.
teunkloosterman at hotmail dot com
30-Aug-2004 05:14
Just to show how it sorts:

<?php
$array
= Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "!", "@", "#", "\\\$", "%", "^", "&", "*", "(", ")", "_", "-", "=", "+", "\\\\", "|", ",", "<", ".", ">", "?", "'", "\\\"", "`", "~");
sort($array);
echo
implode("", $array);
?>

returns:

 !"#$%&'()*+,-.0123456789<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ
\^_`abcdefghijklmnopqrstuvwxyz|~

note: the result begins with a space
Gerhard Conradi
10-Mar-2004 05:49
Hi @all,

to my previous author: thanks, thanks, thanks! Works great and saved me lot of time. I added an extension for ascending/descending order. Here are the changes:

// first i defined two constants for asc and desc
// you can skip that, but then you have to change the switch command at the end of the function
define("ORDER_ASC", "asc");
define("ORDER_DESC", "desc");

function mu_sort($array, $key_sort, $asc_desc) { // start function
       
// check input parameters
if (func_num_args()<2 || func_num_args()>3) die("Wrong number of parameters for the call of mu_sort()");
       
   $array = func_get_arg(0);
   $key_sort = func_get_arg(1);
   if (func_num_args()==3)
      $asc_desc = func_get_arg(2);
   else
      $asc_desc = ORDER_ASC;

   $key_sorta = explode(",", $key_sort);
   .... // from here it's exactly the same

   ... // until here
      } // end loop #2
   } // end loop #1

   // sort
   // if you don't want to use the constants defined at the top, change here to whatever you're using
   switch($asc_desc) {
      case ORDER_DESC:
         rsort($output); break;
      default:
         sort($output);
   }

   // return sorted array
   return $output;
} // end function

Now you can call the function like this:
mu_sort($array, $key_sort, ORDER_DESC);
or even:
mu_sort($array, $key_sort); // This will sort the array by default ascending

Have fun...
matias at arenasoftware dot com
24-Mar-2003 10:36
Lets say you have the following array:
$array1 = Array(third => "Some text 3", second => "Some text 2", first => "Some text 1", fourth => "Some text 4" );
and you want to sort it by first,second,third, fourth.
Very easy! Just create a second array:
$array2 = Array(first => "", second => "", third => "", fourth => "");
and do:
$result = array_merge($array2,$array1);

now $result = Array(first => "Some text 1",second => "Some text 2",third => "Some text 3",fourth => "Some text 4");

This is very usefull when you want to print data out of a database table for example, but you would like to give it a custom order.
arjan321 at hotmail dot com
01-Feb-2003 05:23
Ik you want to sort case insensitive, use the natcasesort()
shailu_agrawal at yahoo dot no_spam dot com
13-Nov-2002 11:05
if 2nd parameter is "SORT_REGULAR" it will compare characters by ASCII values.
For ex: if unsorted values are:  B , a , A
with SORT_REGULAR sorted result will be: A , B , a
and with SORT_STRING sorted result will be: a , A , B
whit at transpect dot com
26-Oct-2002 06:06
fmmarzoa's qsort_multiarray gets the last record with the removal of a "-1" from the 3rd line of the function definition. Here's a routine using it that takes a tab-delimited table with the column names in the first row, and displays it from an arbitrary column on the left to the last on the right with option to sort by any one column:

<?php                                                                                         
//tab-delimited ASCII file to sort - no more than one linefeed at end                         
$infile="members.txt";
//first column on left to display - count from 0
$firstcol="1";

function
qsort_multiarray($array, $num = 0, $order = "ASC", $left = 0, $right = -1) {
        if(
$right == -1) {
               
$right = count($array);
        }
       
$links = $left;
       
$rechts = $right;
       
$mitte = $array[($left + $right) / 2][$num];
        if(
$rechts > $links) {
                do {
                        if(
$order == "ASC") {
                                while(
$array[$links][$num]<$mitte) $links++;
                                while(
$array[$rechts][$num]>$mitte) $rechts--;
                        } else {
                                while(
$array[$links][$num]>$mitte) $links++;
                                while(
$array[$rechts][$num]<$mitte) $rechts--;
                        }
                        if(
$links <= $rechts) {
                               
$tmp = $array[$links];
                               
$array[$links++] = $array[$rechts];
                               
$array[$rechts--] = $tmp;
                        }
                } while(
$links <= $rechts);
                if (
$left < $rechts) $array = qsort_multiarray($array,$num,$order,$left, $rechts);
                if (
$links < $right) $array = qsort_multiarray($array,$num,$order,$links,$right);
        }
        return
$array;
}

$mem=file("$infile");
$line=explode("\t","$mem[0]");
$cnt=count($line);
echo
"<html><head><title>$infile</title></head><body>";
echo
"<table border=\"1\"><tr>";
for (
$i=$firstcol;$i<$cnt;$i++) {
        echo
"<td><a href=\"$PHP_SELF?col=$i\">$line[$i]</a></td>";
}
echo
"</tr>";
$cnt=count($mem);
for (
$i=$firstcol;$i<$cnt;$i++) {
       
$line=explode("\t","$mem[$i]");
       
$cnt2=count($line);  
        for (
$j=$firstcol;$j<$cnt2;$j++) {
               
$mem2[$i][$j]=$line[$j];
        }
}
if(!isset(
$col)) $col=$firstcol;
$mem2=qsort_multiarray($mem2,$col);
for (
$i=0;$i<$cnt;$i++) {
        echo
"<tr>";
        for (
$j=$firstcol;$j<$cnt2;$j++) {
               
$show=$mem2[$i][$j];
                echo
"<td>$show</td>";
        }
        echo
"</tr>";
}
echo
"</table></body></html>";
?>
15-Mar-2002 07:09
If you want to sort an array according to your locale do the following:
(de_DE as example)

setlocale("LC_ALL","de_DE");
usort($arraytobesorted, 'strcoll');

--ed: In PHP 4.4 and PHP 5.0 you can do:
setlocale("LC_ALL", "de_DE");
sort($arraytobesorted, SORT_LOCALE_STRNG);
RunMaster at gmx dot de
30-Aug-2001 02:11
Note that sort() is void, so an assignment like

$foo_array = sort( $foo_array );

wont work. In result you cannot combine sort() for use with other array-related functions just like array_unique() or array_values(). None of the following statements works:

sort( array_unique( $foo_array ) );
// returns empty string

$foo_array = array_unique( sort( $foo_array) );
// returns parameter failure

The only way to combine those functions is to apply sort() in a single line statement.

So please dont waste your time with hours of debugging like me before I found out, its void... ;-)
phpdotnetNO_SPAM at electronic-strategy dot com
06-Jul-2001 01:34
/*
Small function to Alphabetically sort Multidimensional arrays by index values of an n dimension array.

I have only tested this for sorting an array of up to 6 dimensions by a value within the second dimension. This code is very rough and works for my purposes, but has not been tested beyond my needs.

Although a little clunky and not a mathematical type algorithm, it get's the job done. It theoretically overcomes many of the problems I have seen with multidimensional arrays in that it is possible to specify within the function, not by reference :-(, which index you wish to sort by, no matter how many dimensions down.

call function by assigning it to a new / existing array:

$row_array = multidimsort($row_array);
*/

function multidimsort($array_in) {
     $multiarray = array();
    $array_out = array();
    $loopvalue = 0;
   
    /* -1 as traversal of array starts from 0, count() starts from 1 */
    $multicount = count($array_in) - 1;

    /* add the indexes you wish to sort array by to a new array in this case index is two levels down, but shouldn't make a difference if it goes further indexes down. (Not tested!) */
    for($i = 0; $i <= $multicount; $i++) {
        array_push($multiarray, $array_in[$i][2]);
        //array_push($multiarray, $array_in[$i][2][4]);
        //array_push($multiarray, $array_in[$i][1][3][7]);
    }
   
    /* alphabetically sort the new array (Ascending in this case) can chage sort to whatever type you like. Even apply user-defined sort. */
    asort($multiarray);
   
    /* reset internal pointer to beginning of array after above sort */
    reset($multiarray);
   
    /* traverse new array of index values and add the corresponding element of the input array to the correct position in the output array */
    while (list ($key, $val) = each ($multiarray)) {
       
        $array_out[$loopvalue] = $array_in[$key];
       
        $loopvalue++;
    }

    /* return the output array which is all nicely sorted by the index you wanted! */
    return $array_out;
}
Richard dot C dot Mitchell at Boeing dot com
24-Apr-2001 01:59
The usort function can be used to sort multi-dimension arrays, also.

To do a case-insensitive sort on the 7th column of a two-dimensional array:
usort($listing, create_function('$a,$b','return strcasecmp($a[7],$b[7]);'));

When using associative arrays:
usort($listing, create_function('$a,$b','return strcasecmp($a["name"],$b["name"]);'));

Case-sensitive, descending (just change '$a,$b' to '$b,$a'):
usort($listing, create_function('$b,$a','return strcmp($a["name"],$b["name"]);'));

A two-dimensional array of numbers (7th column, ascending):
usort($listing, create_function('$a,$b','return $a[7]==$b[7]?0:($a[7]<$b[7]?-1:1);'));
peek at mailandnews dot com
07-Apr-2001 08:06
I ran into the same problem with case insensitive sorting. Actually I think there should be a SORT_STRING_CASE flag but I tried the following:

usort($listing, 'strcasecmp');

This didn't work (why not?), but you can do a proper case insensitive sort like this:

usort($listing, create_function('$a,$b','return strcasecmp($a,$b);'));
ultrafunkula at mad dot scientist dot com
31-Mar-2000 03:08
What you really want is asort(). The neat thing about PHP arrays is that even though they have a traditional numerical index, they are not defined by it. What I mean by this is if you define an array $data[], then $data[2] does not necessarily exist between $data[1] and $data[3].
So if you asort() by the artist name and then use each() to recover the array elements in the order they exist(rather than an incremental loop which ties you to their arbitrary numerical key), you can sort your data by any dimension of your array. Try this out:
$data[0][1]='Title 2';
$data[0][2]='Title 1';
$data[0][0]='Title 3';
$data[1][1]='Barney';
$data[1][0]='Charlie';
$data[1][2]='Al';
$data[2][2]='Sing the song of Al';
$data[2][1]='Jam with Barney';
$data[2][0]='Charlie Rocks';

while (list($key) = each($data[1])) {
print $data[0][$key].",".$data[1][$key].",".$data[2][$key]."<p>";
}

asort($data[1]);

while (list($key) = each($data[1])) {
print $data[0][$key].",".$data[1][$key].",".$data[2][$key]."<p>";
}

The interesting part is that the first, UNSORTED array doesn't come out in numerical order, because it wasn't put in that way.  If you asort($data[2]) you'll sort by lyrics, and so on. Just make sure you remember to each() by the same dimension as you asort().
misillet at tin dot it
18-Mar-2000 03:58
Note that using sort() it seems to sort by ASCII code, because "AC" is before "Ab" in the result array

uasort" width="11" height="7"/> <sizeof
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites