```python
# Using a lambda function helps to customize the sorting key for better performance.
numbers = [1, -5, 10, 6, 3, -4, -9]
# Sort the list based on the absolute values of the numbers in descending order
sorted_numbers = sorted(numbers, key=lambda x: abs(x), reverse=True)
print(sorted_numbers)
| Associated Context | |
| ------------------------------------------ | ---------------------------------------- |
| Type | Code Snippet ( ***.py*** ) |
| Associated Tags | `Sorted list` `Sorting algorithm` `Key-value pairs` `Reverse sorting` `Data manipulation` `Python programming` |
| π Custom Description | Sort a list in python<br><br>I have this list<br><br>[1,-5,10,6,3,-4,-9]
<br>But now I want the list to be sorted like this:<br><br>[10,-9,6,-5,-4,3,1]
<br>As you can see I want to order from high to low no matter what sign each number has, but <br> |
| π‘ Smart Description | The code snippet sorts an array of numbers in ascending order and returns the sorted list as a tuple. The result is then returned with all negative values from 1 to 10, -9<br> |
| π Suggested Searches | Python sorted function example |
| Related Links | [https://stackoverflow.com/questions/19199984/sort-a-list-in-python](https://stackoverflow.com/questions/19199984/sort-a-list-in-python)<br>[https://code.pieces.app/onboarding/chrome/welcome](https://code.pieces.app/onboarding/chrome/welcome)<br>[https://code.pieces.app/plugins](https://code.pieces.app/plugins)<br>[https://www.google.com/search?q=python+sort+list+stack+overflow&oq=python+sort+list+stac&gs_lcrp=EgZjaHJvbWUqBwgAEAAYgAQyBwgAEAAYgAQyBggBEEUYOTIICAIQABgWGB4yDQgDEAAYhgMYgAQYigUyDQgEEAAYhgMYgAQYigUyDQgFEAAYhgMYgAQYigUyBggGEEUYPKgCALACAA&sourceid=chrome&ie=UTF-8](https://www.google.com/search?q=python+sort+list+stack+overflow&oq=python+sort+list+stac&gs_lcrp=EgZjaHJvbWUqBwgAEAAYgAQyBwgAEAAYgAQyBggBEEUYOTIICAIQABgWGB4yDQgDEAAYhgMYgAQYigUyDQgEEAAYhgMYgAQYigUyDQgFEAAYhgMYgAQYigUyBggGEEUYPKgCALACAA&sourceid=chrome&ie=UTF-8)<br>[https://www.geeksforgeeks.org/python-lists/](https://www.geeksforgeeks.org/python-lists/)<br>[https://www.geeksforgeeks.org/python-list-sort-method/](https://www.geeksforgeeks.org/python-list-sort-method/) |
| Related People | [Deepak Sisupalan](mailto:[email protected]), [Deepak Sisupalan](mailto:[email protected]) |
| Sensitive Information | No Sensitive Information Detected |
| Shareable Link | https://user-66a01386-192b-44a7-9799-37bb0c525ce9-hj3fjcu2da-uc.a.run.app/?p=59f648866e |