> On 19 Jan 2015, at 05:00, Andrea Faulds <a...@ajf.me> wrote: > > Related: since we have no Perl-like spaceship operator ($a <=> $b), writing > comparison functions is unnecessarily complex in the common case, as you must > produce -1, 0, 1 yourself. > > Could we expose a cmp() or compare() function that calls our internal > comparison operator? This would make writing custom sort functions a lot > nicer, and quite possibly improve some other kinds of code. > > That would also mean a future sorting API could unify user sorts and non-user > sorts: just make the default callback be cmp(). > > Usage would be like this: > > cmp(1, 2); // 1 > cmp(1, 1); // 0 > cmp(2, 1); // 1 > > Essentially, exactly like the spaceship in Perl, but a function. > > Thoughts?
I went ahead and implemented it, since it’s so trivial: https://github.com/php/php-src/pull/1006 Should I write an RFC, or could we just merge this without needing one? I’m also wondering if numcmp(), to do “numeric comparison” might also be a good idea. Currently it’s the same as cmp((float)$val1, (float)$val2); but that might (and probably should, that leads to inaccurate sorting!) change in future. Thoughts? -- Andrea Faulds http://ajf.me/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php