In [ ]:
#Sorting
#The sort function can be used to sort a list in ascending, descending or user defined order.
In [3]:
#list
my_list=[9,1,8,2,7,3,6,4,5]
In [4]:
#sorted function returns the sorted list
sort_list= sorted(my_list)
In [5]:
print("Sorted List:\t",sort_list)
print("Original List:\t", my_list)
Sorted List:	 [1, 2, 3, 4, 5, 6, 7, 8, 9]
Original List:	 [9, 1, 8, 2, 7, 3, 6, 4, 5]
In [6]:
#To sort the original list, you can use sort method. It doesnot returns the list just sort the original one.
my_list.sort()
In [7]:
#Original List gets sorted
print("Original List:\t", my_list)
Original List:	 [1, 2, 3, 4, 5, 6, 7, 8, 9]
In [19]:
#To Sort in Descending Order with sorted function
my_list=[9,1,8,2,7,3,6,4,5]
sort_list= sorted(my_list, reverse=True)
print("Sorted List:\t",sort_list)
Sorted List:	 [9, 8, 7, 6, 5, 4, 3, 2, 1]
In [20]:
#To Sort in Descending Order with sort method
my_list.sort(reverse=True)
print("Original List:\t", my_list)
Original List:	 [9, 8, 7, 6, 5, 4, 3, 2, 1]
In [21]:
#tuples dont have sort method
my_tup=(9,1,8,2,7,3,6,4,5)
my_tup.sort()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-3430d026d9d6> in <module>
      1 my_tup=(9,1,8,2,7,3,6,4,5)
----> 2 my_tup.sort()

AttributeError: 'tuple' object has no attribute 'sort'
In [23]:
#you can use sorted function to sort but it will return it as a list
my_tup=(9,1,8,2,7,3,6,4,5)
sorted_tup = sorted(my_tup)
print("Tuple:\t",sorted_tup)
Tuple:	 [1, 2, 3, 4, 5, 6, 7, 8, 9]
In [27]:
#Dictionary
#you can use sorted function to sort but it will return it as a list of keys
my_dic={'name':'Kushagra','age':23, 'phone':9878856899,'location':'delhi'}
sorted_dic = sorted(my_dic)
print("Sorted Dic:\t", sorted_dic)
Sorted Dic:	 ['age', 'location', 'name', 'phone']
In [25]:
#Dictionary dont have sort method
my_dic.sort()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-25-14d329268371> in <module>
----> 1 my_dic.sort()

AttributeError: 'dict' object has no attribute 'sort'
In [28]:
li=[-6,-5,-4,1,2,3]
s_li=sorted(li)
print(s_li)
[-6, -5, -4, 1, 2, 3]
In [29]:
#using abs, you can ignore -ve sign
s_li=sorted(li,key=abs)
print(s_li)
[1, 2, 3, -4, -5, -6]
In [30]:
##################
In [31]:
class Employee():
    def __init__(self,name,age,salary):
        self.name=name
        self.age=age
        self.salary=salary
        
    def __repr__(self):
        return f"({self.name}, {self.age}, ${self.salary})"
    
e1=Employee('Carl',37,70000)
e2=Employee('Sarah',29,80000)
e3=Employee('John',43,90000)

employees=[e1,e2,e3]
print(employees)
[(Carl, 37, $70000), (Sarah, 29, $80000), (John, 43, $90000)]
In [32]:
#this won't work because it doesnot know on which element to sort
s_employees=sorted(employees)
print(s_employees)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-01f90df927a1> in <module>
----> 1 s_employees=sorted(employees)
      2 print(s_employees)

TypeError: '<' not supported between instances of 'Employee' and 'Employee'
In [33]:
#making a sort function to used in a key
def e_sort(emp):
    return emp.name

s_employees=sorted(employees,key=e_sort)
print(s_employees)
[(Carl, 37, $70000), (John, 43, $90000), (Sarah, 29, $80000)]
In [37]:
#using lambda function
s_employees=sorted(employees,key=lambda e:e.salary, reverse=True)
print(s_employees)
[(John, 43, $90000), (Sarah, 29, $80000), (Carl, 37, $70000)]
In [36]:
#using attrgetter
from operator import attrgetter
s_employees=sorted(employees,key=attrgetter('age'))
print(s_employees)
[(Sarah, 29, $80000), (Carl, 37, $70000), (John, 43, $90000)]