Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just do (x>y)-(x<y). It's actually pretty fast: http://stackoverflow.com/questions/10996418/efficient-intege...

Disclaimer: not actually my original invention.



That's a good one for integers and if you don't intend the comparator to be inlined. For inlined comparison, I prefer if (x == y) return 0; return (x < y) ? -1 : 1;. The control flow is more transparent and C compilers are usually able to perform the if/if conversion necessary to get the single comparison you'd expect in hand-rolled sorts or searches.




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

Search: