Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: sort of misunderstanding of sortby hv (Prior) |
on Mar 21, 2023 at 11:35 UTC ( [id://11151094]=note: print w/replies, xml ) | Need Help?? |
Generally, sort functions are designed to do sorting as efficiently as possible, typically in O(n log(n)) comparisons. I'm not aware of any way to force perl's sort to do all n(n - 1) / 2 comparisons: if the purpose is not sorting, best not use sort() to do it. The results from the comparator are intended to describe an ordering on the elements: compare(A, B) < 0 asserts that A < B, etc. There is an expectation of consistency here, so we expect identities like these to hold:
Note this important paragraph skulking near the end of perldoc -f sort:
For a bit more about what can go wrong, see the accepted response on this related C++ question: What will std::sort do if the comparison is inconsistent?. I'm not aware that perl's implementation is at risk of crashing, but you are certainly entitled to get weird results.
In Section
Meditations
|
|