Built with Tutify.io

    AIStudyLabs

    Python sorted() Function

    Python sorted() Function

    The sorted() function in python is used to get the sorted list of the specified iterable object. The iterable can be a list, tuple, string, dictionary, set, and frozenset. This function takes three parameters iterable, key, and reverse. The key is used to call a function and the reverse is used to change the sorting order.

    Related: list.sort() Function

    <a id="h-1-sorted-function-syntax"></a>

    1 Sorted Function Syntax

    Following is the syntax of the sorted() function in Python.

    
    # Syntax
    sorted(iterable, key=key, reverse=reverse)
    

    1.1 Parameters of Sorted()

    2. Order Python List using sorted() Function

    By default python sorted function orders in ascending order for numbers and alphabetical order for string data types. This function doesn't order the existing list instead it returns the sorted list. The below example takes the list and input and returns the sorted list.

    
    # List sorted in ascending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    print(technology)
    sorted_tech = sorted(technology)
    print(sorted_tech)
    
    # Output:
    # ['Java', 'Hadoop', 'Spark', 'Pandas', 'Pyspark', 'NumPy', 'Hyperion']
    # ['Hadoop', 'Hyperion', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark']
    

    3. Order Python Set using sorted() Function

    Similarly, the below example takes the set as input and returns the sorted list.

    
    # Set sorted in ascending order
    technology = {'Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'}
    print(technology)
    sorted_tech = sorted(technology, reverse=True)
    print(sorted_tech)
    
    # Output:
    # ['Java', 'Hadoop', 'Spark', 'Pandas', 'Pyspark', 'NumPy', 'Hyperion']
    # ['Hadoop', 'Hyperion', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark']
    

    4. Descending Order using sorted()

    The sorted() function with reverse=True in Python is used to sort an iterable (such as a list, tuple) in descending order. The function returns a new sorted list, leaving the original sequence unchanged. Here is an example of sorting lists in reverse order using sorted() function.

    
    # List sorted in reverse order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    print(technology)
    sorted_tech = sorted(technology, reverse=True)
    print(sorted_tech)
    
    # Output:
    # ['Java', 'Hadoop', 'Spark', 'Pandas', 'Pyspark', 'NumPy', 'Hyperion']
    # ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop']
    

    5. Order by using Key Param

    The Python sorted function takes the optional argument key, that can be used to call a function. In the below example, I am calling the len() to get the length of the string. Here, the list is sorted based on the length of the strings, with the shortest string coming first and the longest string coming last.

    Note that the key can also take a user-defined function to sort the elements in the custom order.

    
    # Sort list by length of strings 
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology.sort(key = len)
    print(technology)
    
    # Output
    # ['Java', 'Spark', 'NumPy', 'Hadoop', 'Pandas', 'Pyspark', 'Hyperion']
    

    6. Sorted Dictionaries

    Finally, passing the dictionary to the sorted() function returns a list with the dictionary key sorted.

    
    # Sort dictionary keys
    technology = {"Z":"1","G":"2","A":"3"}
    print(technology)
    sorted_tech = sorted(technology)
    print(sorted_tech)
    
    # Output:
    # {'Z': '1', 'G': '2', 'A': '3'}
    # ['A', 'G', 'Z']
    

    Conclusion

    In this article, you have learned the syntax and examples of using the sorted() function in python. The sorted() function is used to get the sorted list of iterable objects like list, set, dictionary e.t.c. By using this you can perform the sorting in reverse order and even custom sort.

    For more examples of functions refer to Python Built-in Functions.

    Related Articles

    References