Re: sorting arrays
| From: | Jochem Maas | Date: | Wed, 09 Mar 2005 07:26:47 +0000 |
| Subject: | Re: sorting arrays | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-210289@lists.php.net to get a copy of this message | ||
Gabino Travassos wrote:
Hello List I've looked at array_multisort( ), but it doesn't seem like the right function. Here's an example of my array, it's a listing of CD reviews: $cds=array(array("Lowest of the Low", 20050105,9,"Toronto"), array("Heavy Blinkers",20041020,6,"Chicago"), array("Apple,Fiona",20050308,5,"Seattle")); If I wanted to sort them alphabetically, no problem, just sort($cds).does that even work? sort() sorts the values but the values are all arrays, how does php determine the correct order of a set of arrays?
If I want to sort them by date (the second item in the array) or by ranking (the third), how do I do that? Is there something simple?here is a class I knocked up to order a simple multi-dimensional array.... its not the be all and end all but hopefully its useful to you (maybe you learn something new) examples are at the bottom: <?php /** * MDASort.class.php :: simple interface to sort a mutlidimensional array * * We often have arrays of arrays where the sub-arrays are rows of data * (either created or extracted from a database) - this class allows us to * easily sort arrays in the base container array by any number of keys found * in the sub-arrays. be aware that it is assumed that the array keys found in the * sub arrays are associative. Also maybe the _sortcmp method could be enhanced to * allow arbitrary levels of arrays to be sorted: by calling sort() on a level * N array also the sortKeys contents would then have to be checked to see if * they applied to the current (sub-)array *
* @author Some guy on PHP comment board. <http://php.net/manual/en/function.usort.php> * @author Jochem Maas <jochem@iamjochem.com>*/ /** * This file and its contents is not copyrighted; * The contents are free to be used by anybody under any conditions. */ class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys; //the order in which we want the array to be sorted.
function __construct()
{
if ($cnt = func_num_args()) {
$args = func_get_args();
if (isset($args[0])) {
$this->setData($args[0]);
}
if (isset($args[1])) {
$this->setSortKeys($args[1]);
}
if (isset($args[2]) && $args[2] === true) {
$this->sort();
}
}
}
function _sortcmp($a, $b, $i=0)
{
$r = strnatcmp($a[$this->sortKeys[$i][0]],$b[$this->sortKeys[$i][0]]);
if ($this->sortKeys[$i][1] == "DESC") $r = $r * -1;
if($r==0) {
$i++;
if ($this->sortKeys[$i]) $r = $this->_sortcmp($a, $b, $i);
}
return $r;
}
function sort()
{
if(count($this->sortKeys)) {
usort($this->dataArray, array($this,"_sortcmp"));
}
}
function setData($dataArray = array())
{
$this->dataArray = $dataArray;
}
function setSortKeys($sortKeys = array())
{
$this->sortKeys = $sortKeys;
}
function getData()
{
return $this->dataArray;
}
function getSortKeys()
{
return $this->sortKeys;
}
}
/* example of usage */
/*
$sorter = new MDASort;
$sorter->setData( array(
array("name" => "hank", "headsize" => "small", "age" => 32),
array("name" => "sade", "headsize" => "petit", "age" => 36),
array("name" => "hank", "headsize" => "large", "age" => 33),
array("name" => "sade", "headsize" => "large", "age" => 32),
array("name" => "john", "headsize" => "large", "age" => 32),
array("name" => "hank", "headsize" => "small", "age" => 36),
array("name" => "hank", "headsize" => "small", "age" => 40)
));
$sorter->setSortKeys( array(
array('name','ASC'),
array('headsize','DESC'),
array('age','ASC'),
));
$sorter->sort();
$sortedArray = $sorter->getData();
*/
/* 2nd example of usage */
/*
$data = array(
array("name" => "hank", "headsize" => "small", "age" => 32),
array("name" => "sade", "headsize" => "petit", "age" => 36),
array("name" => "hank", "headsize" => "large", "age" => 33),
array("name" => "sade", "headsize" => "large", "age" => 32),
array("name" => "john", "headsize" => "large", "age" => 32),
array("name" => "hank", "headsize" => "small", "age" => 36),
array("name" => "hank", "headsize" => "small", "age" => 40)
);
$sort = array(
array('name','ASC'),
array('headsize','DESC'),
array('age','ASC'),
);
$sorter = new MDASort($data, $sort);
$sorter->sort();
$sortedArray = $sorter->getData();
*/
/* 3rd example of usage */
/*
$data = array(
array("name" => "hank", "headsize" => "small", "age" => 32),
array("name" => "sade", "headsize" => "petit", "age" => 36),
array("name" => "hank", "headsize" => "large", "age" => 33),
array("name" => "sade", "headsize" => "large", "age" => 32),
array("name" => "john", "headsize" => "large", "age" => 32),
array("name" => "hank", "headsize" => "small", "age" => 36),
array("name" => "hank", "headsize" => "small", "age" => 40)
);
$sort = array(
array('name','ASC'),
array('headsize','DESC'),
array('age','ASC'),
);
$sorter = new MDASort($data, $sort, true); // auto sort
$sortedArray = $sorter->getData();
*/