Aug-09-2018, 09:37 PM
Sure, sorted() as your using it generates a new list without altering the old one so try:
import operator from datetime import datetime phones = [('Samsung S7', '11 March 2016'), ('Samsung S8','29 March 2017'), ('Samsung S9', '16 March 2018'), ('Google Pixel', '20 October 2016'),('Google Pixel 2', '21 October 2017'), ('Apple iPhone 7', '7 September 2016'),('Apple iPhone 8','1 September 2017'),('Apple iPhone 9', '12 September 2018')] def get_key(phone_record): return datetime.strptime(phone_record[1], '%d %B %Y') print("Before sorting\n", phones) sorted(phones, key=get_key) print("\nAfter sorting\n", phones) x = sorted(phones, key= get_key) print(x)