Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread Robert Cummings
On Thu, 2008-12-11 at 11:44 +1300, German Geek wrote:
> On Thu, Dec 11, 2008 at 6:43 AM, Robert Cummings <[EMAIL PROTECTED]>wrote:
> 
> > On Thu, 2008-12-11 at 01:31 +1300, German Geek wrote:
> > > On Wed, Dec 10, 2008 at 10:27 PM, Stut <[EMAIL PROTECTED]> wrote:
> > >
> > > > On 10 Dec 2008, at 04:15, German Geek wrote:
> > > >
> > > >> I need to sort an array of objects. I found this ( at a url that didnt
> > let
> > > >> me send this msg... ) and I would know how to do it, but I believe
> > there
> > > >> might be a cleaner, more elegant way to do it. In Java, you just need
> > to
> > > >> implement the interface Comparable and provide a method called
> > compareTo
> > > >> (as
> > > >> far as i remember) and then you can use one of the many sorting
> > algorithms
> > > >> generically on objects that are comparable...
> > > >>
> > > >> Anyway, I didn't find something like that for PHP. Since I'm using
> > > >> symfony,
> > > >> I had a bit of a play with the objects at hand and simply did a
> > > >> sort($arrayOfObjects) and it did sort them by the id. Just wondering
> > where
> > > >> it got the information on what to sort on (not quite) correctly for my
> > > >> case?
> > > >>
> > > >
> > > > I'm confused. The function you need is the one you mention in the
> > subject.
> > > > All you need to do is create a function that compares two of the
> > objects, in
> > > > whatever way you need it to, and returns -1, 0 or 1. The pass that to
> > the
> > > > usort function - it doesn't care what type of thing is in the array.
> > Full
> > > > details available at http://php.net/usort.
> > >
> > >
> > >
> > > I just ment to say it would be nice to have it like in Java or C# where
> > you
> > > can implement an interface, called Comparable and define a function in
> > the
> > > class called compareTo which returns an integer smaller, equal or greater
> > > than 0 by which elements can generically be sorted by in a collection.
> > Since
> > > PHP has arrays (hash maps) as the primary collection type, and they are
> > very
> > > good for storing sets or other collections, it would be nice to have a
> > sort
> > > function that would look, if the elements of the array have a compareTo
> > > method (implement the Comparable interface), and if they do, then sort
> > with
> > > that. I find it a bit surprising that such a well designed programming
> > > language doesn't have such a useful feature. I guess one could use one of
> > > the gazillion libraries out there to do the same thing. Also, one could
> > > argue that this further checking would slow down functions that are
> > > primarily used for sorting strings. However, the answer could be also in
> > the
> > > ArrayObject class which is in php natively. Only it should implement all
> > the
> > > array functions that are there anyway, which shouldnt be too hard to do
> > for
> > > the PHP PL developers. Some more in depth documentation of that class
> > would
> > > also be helpful.
> > >
> > > Anyway, I found a not perfect, but good enough solution:
> > >
> > > Implement a static compare function in the class of the object and put a
> > > function in my library, that is simply called myTools::sort that will get
> > > the object class of the first element of the array (if there is one) and
> > > sort according to the compare method implemented in that class (if it
> > > exists), otherwise just use sort. The compare method takes the parameters
> > as
> > > self:
> > >
> > > public class AClass { // implements Comparable {
> > >   public static function compare(self $obj1, self $obj2) {
> > > return strcmp($obj1->prop1, $obj2->prop1); // e.g.
> > >   }
> > > }
> > >
> > > That way I will get an exception if there is an object in the array which
> > > does not have that class, but i can live with that. At least i'll get an
> > > exception and not another funny error, because the parameters are self,
> > > right?
> > >
> > > It might be better to define an interface called Comparable with that
> > > method, but was not really necessary for my case.
> > >
> > > Just a few thoughts to maybe improve PHP in the future. Hopefully there
> > will
> > > be a lot of interfaces and objects for collection types at some stage in
> > PHP
> > > natively, although that might clutter the namespace and could be realised
> > > with libraries. What are your thoughts?
> >
> > You can already do what you want. Implement it yourself. Not everyone
> > wants this level of cruft and inefficiency.
> 
> 
> Inefficiency for me is when it takes longer to code. For one second of
> coding time I can waste 1000ms of processing time without any cost. Think
> about what a computer can do in 1000ms. Calling a function generically takes
> next to nothing in processing time (maybe 0.5ms or less. In fact some db
> queries take less than that in my experience...

Regardless of your personal implementation preferences, PHP offers a
sorting mechanism, it's up to you to massage it into your personal
parad

Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread German Geek
On Thu, Dec 11, 2008 at 12:28 PM, <[EMAIL PROTECTED]> wrote:

>
> > Inefficiency for me is when it takes longer to code.
>
> How long can this take?

That's why i like PHP. It's very quick to do stuff in, even if arrays are
not always the ultimate data structure, they're easy to handle with all the
nice functions in PHP...

>
>
> Even if you go full-blown with an Interface and static methods that have to
> be fleshed out in the implementations, you're still talking about an hour or
> so.

Quit complaining and start typing.
> :-)


I wasn't complaining at all. In fact, it was a suggestion to think about. I
don't have a problem with someone proving me wrong. In fact, if i would
think I was always right, i wouldn't write to a mailing list to hear what
other people think. ;-)


> > PHP is a scripting language.
>
Well done. I wasn't aware of that :-)

>
> > Everytime the compiler has to parse the source.
>
> No.
>
> The source is compiled once, and your callback is a PHP function pointer
> passed down to the C function for usort.
>
> That C function has to call back out to the PHP function pointer.
>
> That is "slow" compared to a all native C, perhaps, but it's not
> re-compiling your PHP source function on every call to the compare function.
>
> Even if you are comparing across script calls, APC or ZendCache or similar
> will compile once.
>
> The slowness isn't even in the compiling anyway, really.  It's in hitting
> the disk drive to LOAD the PHP source.
>
> It's just as easy to cache the parsed byte-code as it is the source, and it
> saves a few more cycles, so the caches store the byte-code; But the real
> savings is not hitting the hard drive to get the PHP source.
>
> > You can not except true OOP performance.
>
> If you REALLY want performance, OOP has enough overhead that you can
> re-factor to strictly procedural or functional and squeeze out a bit more
> :-)
>
OOP has less overhead in development time, which is a lot more expensive
than processing time these days. The overhead is O(n). It depends more on
your algorithm than on the way you write functions or the PL i think. In
fact, in some cases the processing would be quicker to not write a function
for a few lines of code. I try to write everything in a function to make it
more readable and maintainable, because that is what really counts, i think.


>
> Not too many device drivers written in C++
>
For device drivers performance is more crucial than for end user
applications. Each ms or even ns you save in a device driver can save you a
multitude in an application (the worse the app code, the more important the
performance of the device driver).

>
>
> > OOP behavior is okay.
> > If performance is the main factor, an C extension will do that.
>
> If you're sorting anything large enough for performance to be the main
> factor, it probably belongs in a database, actually...

Yeah, agree. Whenever i can write a query for something, i do that instead.
In this particular case i had at hand however, it was a lot easier to do the
things i wanted when the data (after a rather complex query) is in memory.
Only maybe up to 20 records which shouldn't be too bad.

>
>
> I know somebody somewhere has some custom PHP extension to prove me wrong,
> but that's going to be the exception.

What is the exception? To prove you wrong or the PHP extension? :-)

>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Tim-Hinnerk Heuer

http://www.ihostnz.com


Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread ceo

> Inefficiency for me is when it takes longer to code.



How long can this take?



Even if you go full-blown with an Interface and static methods that have to be 
fleshed out in the implementations, you're still talking about an hour or so.



Quit complaining and start typing.

:-)



> PHP is a scripting language.

> Everytime the compiler has to parse the source.



No.



The source is compiled once, and your callback is a PHP function pointer passed 
down to the C function for usort.



That C function has to call back out to the PHP function pointer.



That is "slow" compared to a all native C, perhaps, but it's not re-compiling 
your PHP source function on every call to the compare function.



Even if you are comparing across script calls, APC or ZendCache or similar will 
compile once.



The slowness isn't even in the compiling anyway, really.  It's in hitting the 
disk drive to LOAD the PHP source.



It's just as easy to cache the parsed byte-code as it is the source, and it 
saves a few more cycles, so the caches store the byte-code; But the real 
savings is not hitting the hard drive to get the PHP source.



> You can not except true OOP performance.



If you REALLY want performance, OOP has enough overhead that you can re-factor 
to strictly procedural or functional and squeeze out a bit more :-)



Not too many device drivers written in C++



> OOP behavior is okay.

> If performance is the main factor, an C extension will do that.



If you're sorting anything large enough for performance to be the main factor, 
it probably belongs in a database, actually...



I know somebody somewhere has some custom PHP extension to prove me wrong, but 
that's going to be the exception.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread Shiplu
PHP is a scripting language.
Everytime the compiler has to parse the source.
You can not except true OOP performance.
OOP behavior is okay.
If performance is the main factor, an C extension will do that.
-- 
Blog: http://talk.cmyweb.net/
Follow me: http://twitter.com/shiplu

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread German Geek
On Thu, Dec 11, 2008 at 6:43 AM, Robert Cummings <[EMAIL PROTECTED]>wrote:

> On Thu, 2008-12-11 at 01:31 +1300, German Geek wrote:
> > On Wed, Dec 10, 2008 at 10:27 PM, Stut <[EMAIL PROTECTED]> wrote:
> >
> > > On 10 Dec 2008, at 04:15, German Geek wrote:
> > >
> > >> I need to sort an array of objects. I found this ( at a url that didnt
> let
> > >> me send this msg... ) and I would know how to do it, but I believe
> there
> > >> might be a cleaner, more elegant way to do it. In Java, you just need
> to
> > >> implement the interface Comparable and provide a method called
> compareTo
> > >> (as
> > >> far as i remember) and then you can use one of the many sorting
> algorithms
> > >> generically on objects that are comparable...
> > >>
> > >> Anyway, I didn't find something like that for PHP. Since I'm using
> > >> symfony,
> > >> I had a bit of a play with the objects at hand and simply did a
> > >> sort($arrayOfObjects) and it did sort them by the id. Just wondering
> where
> > >> it got the information on what to sort on (not quite) correctly for my
> > >> case?
> > >>
> > >
> > > I'm confused. The function you need is the one you mention in the
> subject.
> > > All you need to do is create a function that compares two of the
> objects, in
> > > whatever way you need it to, and returns -1, 0 or 1. The pass that to
> the
> > > usort function - it doesn't care what type of thing is in the array.
> Full
> > > details available at http://php.net/usort.
> >
> >
> >
> > I just ment to say it would be nice to have it like in Java or C# where
> you
> > can implement an interface, called Comparable and define a function in
> the
> > class called compareTo which returns an integer smaller, equal or greater
> > than 0 by which elements can generically be sorted by in a collection.
> Since
> > PHP has arrays (hash maps) as the primary collection type, and they are
> very
> > good for storing sets or other collections, it would be nice to have a
> sort
> > function that would look, if the elements of the array have a compareTo
> > method (implement the Comparable interface), and if they do, then sort
> with
> > that. I find it a bit surprising that such a well designed programming
> > language doesn't have such a useful feature. I guess one could use one of
> > the gazillion libraries out there to do the same thing. Also, one could
> > argue that this further checking would slow down functions that are
> > primarily used for sorting strings. However, the answer could be also in
> the
> > ArrayObject class which is in php natively. Only it should implement all
> the
> > array functions that are there anyway, which shouldnt be too hard to do
> for
> > the PHP PL developers. Some more in depth documentation of that class
> would
> > also be helpful.
> >
> > Anyway, I found a not perfect, but good enough solution:
> >
> > Implement a static compare function in the class of the object and put a
> > function in my library, that is simply called myTools::sort that will get
> > the object class of the first element of the array (if there is one) and
> > sort according to the compare method implemented in that class (if it
> > exists), otherwise just use sort. The compare method takes the parameters
> as
> > self:
> >
> > public class AClass { // implements Comparable {
> >   public static function compare(self $obj1, self $obj2) {
> > return strcmp($obj1->prop1, $obj2->prop1); // e.g.
> >   }
> > }
> >
> > That way I will get an exception if there is an object in the array which
> > does not have that class, but i can live with that. At least i'll get an
> > exception and not another funny error, because the parameters are self,
> > right?
> >
> > It might be better to define an interface called Comparable with that
> > method, but was not really necessary for my case.
> >
> > Just a few thoughts to maybe improve PHP in the future. Hopefully there
> will
> > be a lot of interfaces and objects for collection types at some stage in
> PHP
> > natively, although that might clutter the namespace and could be realised
> > with libraries. What are your thoughts?
>
> You can already do what you want. Implement it yourself. Not everyone
> wants this level of cruft and inefficiency.


Inefficiency for me is when it takes longer to code. For one second of
coding time I can waste 1000ms of processing time without any cost. Think
about what a computer can do in 1000ms. Calling a function generically takes
next to nothing in processing time (maybe 0.5ms or less. In fact some db
queries take less than that in my experience...

-- 
Tim-Hinnerk Heuer

http://www.ihostnz.com


Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread ceo

You can use a (base) object Comparable with a method compareTo as the callback 
function for http://php.net/usort



That gives you 99% of what you want, for the tiny price of having to pass in 
the array('Comparable','compareTo') as the callback arg.



Given that one frequently calls usort and friends on arrays of data that are 
NOT objects at all, and, arguably, even on arrays of disparate objects or 
objects and non-objects, what would you expect to happen?



usort(array(new Foo, new Bar, 42, new Baz, 'expected outcome?'));



Assume Foo, Bar, and Baz all have different/unique incompatible compareTo 
methods.



PHP is not Java.



Trying to force it to do things the Java way means you probably need to spend 
more time understanding the preceding sentence.



:-)



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread Shiplu
may be you can design a class.

interface ISortable{
public sort();
public compare($a,$b);
}
SortableList implements ISortable {
}


-- 
Blog: http://talk.cmyweb.net/
Follow me: http://twitter.com/shiplu

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread Robert Cummings
On Thu, 2008-12-11 at 01:31 +1300, German Geek wrote:
> On Wed, Dec 10, 2008 at 10:27 PM, Stut <[EMAIL PROTECTED]> wrote:
> 
> > On 10 Dec 2008, at 04:15, German Geek wrote:
> >
> >> I need to sort an array of objects. I found this ( at a url that didnt let
> >> me send this msg... ) and I would know how to do it, but I believe there
> >> might be a cleaner, more elegant way to do it. In Java, you just need to
> >> implement the interface Comparable and provide a method called compareTo
> >> (as
> >> far as i remember) and then you can use one of the many sorting algorithms
> >> generically on objects that are comparable...
> >>
> >> Anyway, I didn't find something like that for PHP. Since I'm using
> >> symfony,
> >> I had a bit of a play with the objects at hand and simply did a
> >> sort($arrayOfObjects) and it did sort them by the id. Just wondering where
> >> it got the information on what to sort on (not quite) correctly for my
> >> case?
> >>
> >
> > I'm confused. The function you need is the one you mention in the subject.
> > All you need to do is create a function that compares two of the objects, in
> > whatever way you need it to, and returns -1, 0 or 1. The pass that to the
> > usort function - it doesn't care what type of thing is in the array. Full
> > details available at http://php.net/usort.
> 
> 
> 
> I just ment to say it would be nice to have it like in Java or C# where you
> can implement an interface, called Comparable and define a function in the
> class called compareTo which returns an integer smaller, equal or greater
> than 0 by which elements can generically be sorted by in a collection. Since
> PHP has arrays (hash maps) as the primary collection type, and they are very
> good for storing sets or other collections, it would be nice to have a sort
> function that would look, if the elements of the array have a compareTo
> method (implement the Comparable interface), and if they do, then sort with
> that. I find it a bit surprising that such a well designed programming
> language doesn't have such a useful feature. I guess one could use one of
> the gazillion libraries out there to do the same thing. Also, one could
> argue that this further checking would slow down functions that are
> primarily used for sorting strings. However, the answer could be also in the
> ArrayObject class which is in php natively. Only it should implement all the
> array functions that are there anyway, which shouldnt be too hard to do for
> the PHP PL developers. Some more in depth documentation of that class would
> also be helpful.
> 
> Anyway, I found a not perfect, but good enough solution:
> 
> Implement a static compare function in the class of the object and put a
> function in my library, that is simply called myTools::sort that will get
> the object class of the first element of the array (if there is one) and
> sort according to the compare method implemented in that class (if it
> exists), otherwise just use sort. The compare method takes the parameters as
> self:
> 
> public class AClass { // implements Comparable {
>   public static function compare(self $obj1, self $obj2) {
> return strcmp($obj1->prop1, $obj2->prop1); // e.g.
>   }
> }
> 
> That way I will get an exception if there is an object in the array which
> does not have that class, but i can live with that. At least i'll get an
> exception and not another funny error, because the parameters are self,
> right?
> 
> It might be better to define an interface called Comparable with that
> method, but was not really necessary for my case.
> 
> Just a few thoughts to maybe improve PHP in the future. Hopefully there will
> be a lot of interfaces and objects for collection types at some stage in PHP
> natively, although that might clutter the namespace and could be realised
> with libraries. What are your thoughts?

You can already do what you want. Implement it yourself. Not everyone
wants this level of cruft and inefficiency.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread German Geek
On Wed, Dec 10, 2008 at 10:27 PM, Stut <[EMAIL PROTECTED]> wrote:

> On 10 Dec 2008, at 04:15, German Geek wrote:
>
>> I need to sort an array of objects. I found this ( at a url that didnt let
>> me send this msg... ) and I would know how to do it, but I believe there
>> might be a cleaner, more elegant way to do it. In Java, you just need to
>> implement the interface Comparable and provide a method called compareTo
>> (as
>> far as i remember) and then you can use one of the many sorting algorithms
>> generically on objects that are comparable...
>>
>> Anyway, I didn't find something like that for PHP. Since I'm using
>> symfony,
>> I had a bit of a play with the objects at hand and simply did a
>> sort($arrayOfObjects) and it did sort them by the id. Just wondering where
>> it got the information on what to sort on (not quite) correctly for my
>> case?
>>
>
> I'm confused. The function you need is the one you mention in the subject.
> All you need to do is create a function that compares two of the objects, in
> whatever way you need it to, and returns -1, 0 or 1. The pass that to the
> usort function - it doesn't care what type of thing is in the array. Full
> details available at http://php.net/usort.



I just ment to say it would be nice to have it like in Java or C# where you
can implement an interface, called Comparable and define a function in the
class called compareTo which returns an integer smaller, equal or greater
than 0 by which elements can generically be sorted by in a collection. Since
PHP has arrays (hash maps) as the primary collection type, and they are very
good for storing sets or other collections, it would be nice to have a sort
function that would look, if the elements of the array have a compareTo
method (implement the Comparable interface), and if they do, then sort with
that. I find it a bit surprising that such a well designed programming
language doesn't have such a useful feature. I guess one could use one of
the gazillion libraries out there to do the same thing. Also, one could
argue that this further checking would slow down functions that are
primarily used for sorting strings. However, the answer could be also in the
ArrayObject class which is in php natively. Only it should implement all the
array functions that are there anyway, which shouldnt be too hard to do for
the PHP PL developers. Some more in depth documentation of that class would
also be helpful.

Anyway, I found a not perfect, but good enough solution:

Implement a static compare function in the class of the object and put a
function in my library, that is simply called myTools::sort that will get
the object class of the first element of the array (if there is one) and
sort according to the compare method implemented in that class (if it
exists), otherwise just use sort. The compare method takes the parameters as
self:

public class AClass { // implements Comparable {
  public static function compare(self $obj1, self $obj2) {
return strcmp($obj1->prop1, $obj2->prop1); // e.g.
  }
}

That way I will get an exception if there is an object in the array which
does not have that class, but i can live with that. At least i'll get an
exception and not another funny error, because the parameters are self,
right?

It might be better to define an interface called Comparable with that
method, but was not really necessary for my case.

Just a few thoughts to maybe improve PHP in the future. Hopefully there will
be a lot of interfaces and objects for collection types at some stage in PHP
natively, although that might clutter the namespace and could be realised
with libraries. What are your thoughts?

-- 
Tim-Hinnerk Heuer

http://www.ihostnz.com


Re: [PHP] usort for sorting an array of objects

2008-12-10 Thread Stut

On 10 Dec 2008, at 04:15, German Geek wrote:
I need to sort an array of objects. I found this ( at a url that  
didnt let
me send this msg... ) and I would know how to do it, but I believe  
there
might be a cleaner, more elegant way to do it. In Java, you just  
need to
implement the interface Comparable and provide a method called  
compareTo (as
far as i remember) and then you can use one of the many sorting  
algorithms

generically on objects that are comparable...

Anyway, I didn't find something like that for PHP. Since I'm using  
symfony,

I had a bit of a play with the objects at hand and simply did a
sort($arrayOfObjects) and it did sort them by the id. Just wondering  
where
it got the information on what to sort on (not quite) correctly for  
my case?


I'm confused. The function you need is the one you mention in the  
subject. All you need to do is create a function that compares two of  
the objects, in whatever way you need it to, and returns -1, 0 or 1.  
The pass that to the usort function - it doesn't care what type of  
thing is in the array. Full details available at http://php.net/usort.


-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] usort for sorting an array of objects

2008-12-09 Thread German Geek
Hi Guys,

I need to sort an array of objects. I found this ( at a url that didnt let
me send this msg... ) and I would know how to do it, but I believe there
might be a cleaner, more elegant way to do it. In Java, you just need to
implement the interface Comparable and provide a method called compareTo (as
far as i remember) and then you can use one of the many sorting algorithms
generically on objects that are comparable...

Anyway, I didn't find something like that for PHP. Since I'm using symfony,
I had a bit of a play with the objects at hand and simply did a
sort($arrayOfObjects) and it did sort them by the id. Just wondering where
it got the information on what to sort on (not quite) correctly for my case?

Thanks for your interest.

-- 
Tim-Hinnerk Heuer

http://www.ihostnz.com -- Web Design, Hosting and free Linux Support