Reputation: 5103
I'm trying to sort a list of unknown values, either ints or floats or both, in ascending order. i.e, [2,-1,1.0] would become [-1,1.0,2]. Unfortunately, the sorted() function doesn't seem to work as it seems to sort in descending order by absolute value. Any ideas?
Upvotes: 2
Views: 6586
Reputation: 31
In addition to doublefelix,below code gives the absolute order to me from string.
siparis=sorted(siparis, key=lambda sublist:abs(float(sublist[1])))
Upvotes: 0
Reputation: 1112
I had the same problem. The answer: Python will sort numbers by the absolute value if you have them as strings. So as your key, make sure to include an int() or float() argument. My working syntax was
data = sorted(data, key = lambda x: float(x[0]))
...the lambda x part just gives a function which outputs the thing you want to sort by. So it takes in a row in my list, finds the float 0th element, and sorts by that.
Upvotes: 2