4 ms·
> It is not obvious that you need to include comparative function for a straightforward numerical sort ? No. It's not obvious. Python - a = [6, -2, 2, -7
by Supermancho 6y ago
> It is not obvious that you need to include comparative function for a straightforward numerical sort ?
No. It's not obvious.
Python -
a = [6, -2, 2, -7]
a.sort()
print(a)
PHP -
$arr = [6, -2, 2, -7];
sort($arr);
print_r($arr);
Rust -
let mut vec = vec![6, -2, 2, -7];
vec.sort();
assert_eq!(vec, vec![-7, -2, 2, 6]);
The expected behavior is a default natural number sort.
What other default was chosen? String comparison for some value of Strings, coerced from the integers in some way.
If you have to research a bunch of implicit behavior to understand how sort works for numbers. I believe that's unintuitive by definition (simplest case is more complex than necessary). Fixable with a required flag or specialized functions (eg PHP).
- deleted 6y ago[deleted]
- SamReidHughes 6y agoIt was more intuitive back in the day when everybody knew Perl.