Home > Commands ->  Lists -> List Commands -> sorted


Syntax: list1s=sorted(list1, option) where list1 is list.

     Result: list1s is list1 with the elements sorted. To sort in descending order, use option: reverse=1.


  • Example 1
    list1 = [18,25,23,35,65,54]
    list1s=sorted(list1)
    print list1, list1s
    Result:
    [18, 25, 23, 35, 65, 54] [18, 23, 25, 35, 54, 65]

  • Example 2 - you must define an object or nothing happens
    list1 = [18,25,23,35,65,54]
    sorted(list1)
    print list1
    Result:
    [18, 25, 23, 35, 65, 54]

  • Example 3 - to substitute the sorted list ...
    list1 = [18,25,23,35,65,54]
    list1=sorted(list1)
    print list1
    Result:
    [18, 23, 25, 35, 54, 65]

  • Example 4 - to substitute the sorted list ...
    list1 = [18,25,23,35,65,54]
    list1=sorted(list1)
    print list1
    Result:
    [18, 23, 25, 35, 54, 65]

  • Example 5 - to substitute the sorted list ...
    list1 = ['c','a','c','b']
    list1s=sorted(list1,reverse=1)
    print list1s
    Result:
    ['c', 'c', 'b', 'a']

Related wikipages: Operations on Lists, Commands on Lists, append, sum

My Sagepage: Basic Descriptive Statistics


Keywords: sort, sorted, list, command