Sort a list in python
问题 I have this list [1,-5,10,6,3,-4,-9] But now I want the list to be sorted like this: [10,-9,6,-5,-4,3,1] As you can see I want to order from high to low no matter what sign each number has, but keeping the sign, is it clear? 回答1: Use abs as key to the sorted function or list.sort : >>> lis = [1,-5,10,6,3,-4,-9] >>> sorted(lis, key=abs, reverse=True) [10, -9, 6, -5, -4, 3, 1] 回答2: Use: l.sort(key= abs, reverse = True) Lists can be sorted using the sort() method. And the sort method has a