note 62311 added to function.sort
| From: | jesper at snt dot utwente dot nl | Date: | Fri, 24 Feb 2006 21:26:40 +0000 |
| Subject: | note 62311 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104382@lists.php.net to get a copy of this message | ||
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()
----
Server IP: 194.109.193.119
Probable Submitter: 130.89.174.11
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62311
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62311&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62311&report=yes
Search -- http://master.php.net/manage/user-notes.php