You are here: Re: How to sort array of classes based on the value of property « PHP Programming Language « IT news, forums, messages
Re: How to sort array of classes based on the value of property

Posted by ZeldorBlat on 09/19/06 18:58

Moti.Ba@gmail.com wrote:
> Hello,
>
> I have a class like this one
>
> Class foo
> {
> public $sum;
> }
>
> And I have an array of instances based on this class. When I use
> ksort() the array is sorted by key, next I want to sort it by the $sum
> property.
>
> How can I do it?

Try usort():
<http://www.php.net/usort>

Something like this should work:

function cmpFoo($a, $b)
{
if ($a->sum == $b->sum)
return 0;

return ($a->sum < $b->sum) ? -1 : 1;
}

usort($arr, "cmpFoo"); //$arr is the array of objects

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация