• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What does sorted() do?Basic SyntaxSorting different iterable typesUsing the key parameter for custom sortingUsing the reverse parameterDifference between sorted() and list.sort()Summary
      • Blog

      Python Sorted Function

      What does sorted() do?

      The sorted() function returns a new sorted list from the items in any iterable (list, tuple, string, etc.), leaving the original iterable unchanged.

      Basic Syntax

      sorted(iterable, key=None, reverse=False)
      
      • iterable: The sequence to sort.
      • key: (Optional) A function to customize sorting (e.g., sort by length).
      • reverse: (Optional) If True, sorts in descending order.

      Sorting different iterable types

      numbers = [3, 1, 4, 2]
      print(sorted(numbers))  # Output: [1, 2, 3, 4]
      
      text = "python"
      print(sorted(text))  # Output: ['h', 'n', 'o', 'p', 't', 'y']
      

      Using the key parameter for custom sorting

      words = ["apple", "banana", "cherry"]
      print(sorted(words, key=len))  # Output: ['apple', 'banana', 'cherry']
      print(sorted(words, key=lambda w: w[-1]))  # Sort by last letter
      

      Using the reverse parameter

      numbers = [3, 1, 4, 2]
      print(sorted(numbers, reverse=True))  # Output: [4, 3, 2, 1]
      

      Difference between sorted() and list.sort()

      • sorted() works on any iterable and returns a new list.
      • list.sort() is a method that sorts a list in place and returns None.

      Summary

      • sorted() returns a new sorted list from any iterable.
      • Original data is not changed.
      • Use key for custom sorting and reverse for descending order.
      • Works with numbers, strings, and more.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python List

      For You

      Python Lists: A Complete Introduction Lists are one of the most versatile and commonly used data typ

      Python Functions

      For You

      Organizing Code: Understanding Functions In programming, we often need to perform the same set of ac

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.