Hacker News new | past | comments | ask | show | jobs | submit login

All these features have been discussed countless times in "new release" threads and such, but I don't think I ever asked public opinion on the "spaceship" operator, so I want to raise the question this time. I always ignored it as a feature that I personally don't need, but somebody else does, so ok, whatever.

You see, I actually don't think I ever used usort in about 10 years (or how long is it since anonymous functions were introduced). Instead, in all of my projects there is an implementation of https://underscorejs.org/#sortBy

The difference is that the callback maps the value to something that is already sortable using other native PHP functions, like an integer or a string. I find it way more intuitive than writing these function($a, $b) {/* try to remember which of [-1,0,1] it is supposed to return */} abominations. So as a solutions to remembering "which of [-1,0,1] should it be" I'd expect just natively implementing in C sane sortBy function to replace this pre-historic PHP bullshit. But whatever, it's a matter of taste…

…Or so I thought until now. Now that I think about it, I see [-1,0,1] callback as something really fucked up. PHP usort (and basically all PL ordering functions I know) can order only linear sequences, i.e. it returns an ordered list. Not even a lattice. Basically this means real numbers (or, more comfortable to use in a PL: strings). And with [-1,0,1] I can define whatever the fuck I want. There's nothing to stop me from implementing "paper/scissors/rock" (or even something much more convoluted) in it, and I have no idea how usort will behave then. That doesn't sound good.

So I'd like somebody to persuade me that I personally need [-1,0,1] comparison model for some use case I apparently never encountered. The only reason I can think of why [-1,0,1] might be better is performance (or RAM, actually), but I don't accept it as a valid argument: somehow even my non-optimal sortBy PHP implementation always turns out to be enough, and if I really should worry about sorting performance, PHP probably isn't the right language for my use-case anyway.




Technically they are equivalent, meaning that theoretically you can implement one with the other. The main advantage of sorting with [-1,0,1] is if you are sorting by multiple parameters

Convoluted example: sort some cars by year then by weight except with all Toyota first

In general it is easy to simulate multiple SQL `order by` clauses with [-1,0,1].

Using primitives you can map each car to something like "0t-1994y-0300kg" (remembering to left pad numbers) and sort by that.

In practice they are equivalent (when used correctly) but it is easier to implement primitive sorting with [-1,0,1] than viceversa.

Sometimes I wish languages would have native equality and order operators on (immutable) arrays, so that [1,1]<[1,2]<[2,0]<[2,0,-1] would work...


They are not "technically equivalent", as I've shown above. You can implemnt papre/scissors/rock with [-1,0,1], which is not linear ordering function.

And you didn't show anything for which ordering cannot be defined by mapping objects (with any number of parameters) to strings, which is kind of the whole purpose of sortBy anyway.


> And you didn't show anything for which ordering cannot be defined by mapping objects (with any number of parameters) to strings, which is kind of the whole purpose of sortBy anyway

I did so by intentionally, as i believe that mapping to complex enough strings is enough.

> You can implemnt papre/scissors/rock with [-1,0,1], which is not linear ordering function.

In PHP and in most other languages NaN == NaN, NaN === NaN, NaN <= NaN, and NaN < NaN are all false due to how IEEE floating point numbers are defined so you can have weird stuff going on there too.

With some testing:

$v=[1,2,3,4,5,6]; usort($v,$f);

for:

$f=function($a,$b){ return ($a - $b)%3; }; $f=function($a,$b){ return -($a - $b)%3; }; $f=function($a,$b){ return 1; }; $f=function($a,$b){ return -1; }; $f=function($a,$b){ return 0; };

it appears that once $f($a,$b) is calculated $f($b,$a) is inferred to be -$f($a,$b) and to guarantee $f($v[i],$v[i+1]) <= 0 under that assumption.

But I am not sure... honestly i don't care... I am not arguing that one is better than the other...


I've never used the spaceship operator in my own code, and whenever I see it in someone else's, I almost always have to look it up to remind myself what it does.

I guess it's a nice feature to have, but I tend to forget that it exists. Clearly it's not exactly a new feature I was ever very excited about.

It doesn't help that writing my own sorting callbacks is itself something I rarely need to do.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: