I want to sort a tuple using abs() without actually changing the elements of the tuple to positive values.
def sorting(numbers_array):
return sorted(numbers_array, key = abs(numbers_array))
sorting((-20, -5, 10, 15))
According to the python wiki (https://wiki.python.org/moin/HowTo/Sorting/#Key_Functions), the sorted(list, key=) function is suppose to sort with the parameter key without actually altering the elements of the list. However, abs() only takes int() and I haven't worked out a way to make that tuple into an int, if that's what I need to do.
sorted((-20, -5, 10, 15), key=lambda v: -v if v<0 else v)is an alternate to the more correctsorted((-20, -5, 10, 15), key=abs)- dawg