Python list sort() function is used to sort a list in ascending, descending or user defined order.

To sort the list in ascending order

[pastacode lang=”python” manual=”List_name.sort()%0AThis%20will%20sort%20the%20given%20list%20in%20ascending%20order.%0A” message=”” highlight=”” provider=”manual”/]

This function is used to sort list of integers, floating point number, string and others.

[pastacode lang=”python” manual=”numbers%20%3D%20%5B8%2C%205%2C%207%2C%209%5D%20%0A%20%20%0A%23%20Sorting%20list%20of%20Integers%20in%20ascending%20%0Anumbers.sort()%20%0A%20%20%0Aprint(numbers)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”%5B5%2C%207%2C%208%2C%209%5D” message=”” highlight=”” provider=”manual”/]

Example 2:

[pastacode lang=”python” manual=”strs%20%3D%20%5B%22wikitechy%22%2C%20%22code%22%2C%20%22ide%22%2C%20%22practice%22%5D%20%0A%20%20%0A%23%20Sorting%20list%20of%20Integers%20in%20ascending%20%0Astrs.sort()%20%0A%20%20%0Aprint(strs)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”%5B’code’%2C%20’ide’%2C%20’practice’%2C%20’wikitechy’%5D” message=”” highlight=”” provider=”manual”/]

To sort the list in descending order

Example:

[pastacode lang=”python” manual=”list_name.sort(reverse%3DTrue)%0AThis%20will%20sort%20the%20given%20list%20in%20descending%20order%0A” message=”” highlight=”” provider=”manual”/] [pastacode lang=”python” manual=”numbers%20%3D%20%5B8%2C%205%2C%207%2C%209%5D%20%0A%20%20%0A%23%20Sorting%20list%20of%20Integers%20in%20descending%20%0Anumbers.sort(reverse%20%3D%20True)%20%0A%20%20%0Aprint(numbers)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”%5B9%2C%208%2C%207%2C%205%5D” message=”” highlight=”” provider=”manual”/]

Sorting user using user defined order

[pastacode lang=”python” manual=”list_name.sort(key%3D%E2%80%A6%2C%20reverse%3D%E2%80%A6)%20%E2%80%93%20it%20sorts%20according%20to%20user%E2%80%99s%20choice” message=”” highlight=”” provider=”manual”/]

Example

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20demonstrate%20sorting%20by%20user’s%20%0A%23%20choice%20%0A%20%20%0A%23%20function%20to%20return%20the%20second%20element%20of%20the%20%0A%23%20two%20elements%20passed%20as%20the%20parameter%20%0Adef%20sortSecond(val)%3A%20%0A%20%20%20%20return%20val%5B1%5D%20%20%0A%20%20%0A%23%20list1%20to%20demonstrate%20the%20use%20of%20sorting%20%20%0A%23%20using%20using%20second%20key%20%20%0Alist1%20%3D%20%5B(1%2C%202)%2C%20(3%2C%203)%2C%20(1%2C%201)%5D%20%0A%20%20%0A%23%20sorts%20the%20array%20in%20ascending%20according%20to%20%20%0A%23%20second%20element%20%0Alist1.sort(key%20%3D%20sortSecond)%20%20%0Aprint(list1)%20%0A%20%20%0A%23%20sorts%20the%20array%20in%20descending%20according%20to%20%0A%23%20second%20element%20%0Alist1.sort(key%20%3D%20sortSecond%2C%20reverse%20%3D%20True)%20%0Aprint(list1)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”%5B(1%2C%201)%2C%20(1%2C%202)%2C%20(3%2C%203)%5D%0A%5B(3%2C%203)%2C%20(1%2C%202)%2C%20(1%2C%201)%5D%0A” message=”” highlight=”” provider=”manual”/]

Categorized in: