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

You are right. He was really careful in picking double instead of int for this example.


Actually I would say it was rather careless, because it mishandles NaNs:

    vector<double> v = {0, nan(""), 1};
    sort(v.begin(), v.end(), [](double x, double y) { return x>y; });
In my test this leaves the vector unchanged, which is definitely not in decreasing order.


">" does not form a total order on doubles if you include NaN, so that's not really suprising. That is, you're providing a comparison function which is invalid, you should (at best) expect invalid results.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

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

Search: