I have list ["Tue", "Wed", "Mon", "Thu", "Fri"]
as list, I want to make it as
["Mon", "Tue", "Wed", "Thu", "Fri"]
.
How to sort this?
I have list ["Tue", "Wed", "Mon", "Thu", "Fri"]
as list, I want to make it as
["Mon", "Tue", "Wed", "Thu", "Fri"]
.
How to sort this?
Find and replace all instances of monday with 1, tuesday with 2, etc sort, reassign.
Not very efficient, but if you have a list of the order they're supposed to be in...
Note, this will throw an exception if a certain value is present in
n
that isn't inm
.Or put them into a dict with name as key and order as value, then use
key=your_dict.get
as a key... something like:This won't throw an exception (except on Py3.x where you'll get an error on trying to sort
None
), but you could use a partial to sensible, sort before or after default, or to get equivalent behaviour oflist.index
, usekey=d.__getitem__
or similarhttp://wiki.python.org/moin/HowTo/Sorting/
You are looking for a lexical sort. It’s also interesting to write your own function, as a learn matter ;)
EDIT: ok you just need a function that, for a day, returns its day week number. You can simply use an array of day string in the function, and then use the day week number to sort elements like a boss.