im newbie in Python and i got task to sort dates in dictionary by year, month, day.
I found one nice method from W3Schools but i dont know how to work one thing. Below is code
def myFunc(e):
return e['year']
cars = [
{'car': 'Ford', 'year': 2005},
{'car': 'Mitsubishi', 'year': 2000},
{'car': 'BMW', 'year': 2019},
{'car': 'VW', 'year': 2011}
]
cars.sort(key=myFunc)
print(cars)
Its working good to sort for example cars by year, also to dates by year, month, day but i have question. How this method is working and why there is 'e' which i didnt use in cars.sort(key=myFunc)? Why this 'key=myFunc' myFunc is without argument? Can i write it without this function? My main language is Java so its abstract for me and maybe someone can explain this.
def myFunc(e):
return e['year']
I have read doc about sort but still i dont understand this function.
sortalgorithm will call the function for each element in the list, passing that element as an argument. The function should return the value to use in the sort.