range is defined as range(start, stop, step) and the stop parameter is not included.
sorted defined as sorted(iterable, key=key, reverse=reverse) seems to work like this:
The original input values are outputted but sorted by a key, in this case by their square. The square converts the negative values into positive ones, then sorting is applied and finally, the original values are returned in order.
Tested this on the command line:
print(sorted(range(-3,4)))
# [-3, -2, -1, 0, 1, 2, 3]
print(sorted(range(-3,4), key=lambda x: x*x))
# [0, -1, 1, -2, 2, -3, 3]
The Python Docs about sorting; it says for both sorted() (and also sort()) the sort is stable:
Sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved.