Feb-06-2017, 08:09 PM
(Feb-06-2017, 06:14 PM)snippsat Wrote: What have you tried?
I have tried to get a hint about how to build up a working code - as I had no idea how it should look like. All the things in the SortingHowTo give no (for me understandable) answer to that.
I mean, it's easy to say that
Monday = 'Monday' Tuesday = 'Tuesday' Wednesday = 'Wednesday' Thursday = 'Thursday' Friday = 'Friday' Saturday = 'Saturday' Sunday = 'Sunday' list1 = [Wednesday, Tuesday, Saturday, Monday, Sunday] list2 = [Monday, Tuesday, Friday, Wednesday] list3 = [Sunday, Saturday, Sunday] list4 = [Friday, Tuesday, Thursday] list1.sort() list2.sort() list3.sort() list4.sort() print list1 print list2 print list3 print list4only give the 'correct' result for list3 - and that's kind of 'accidentally' as in the case of Saturday and Sunday, the alphabetic order is the 'correct' one.
(Feb-06-2017, 06:40 PM)Ofnuts Wrote: Your problem is defining what is a rather arbitrary sort order between some strings.
And it becomes even much more 'arbitrary' when I leave the weekday example and go to what I wish, yes. But your code is exactly what I've been looking for, as far as I can say for the moment. I will try and see whether I can make my code working in that way.
My own approach was going to something like
def sortdays(day1, day2): if day1 == Monday and day2 == Tuesday: return -1 elif day1 == Monday and day2 == Monday: return 0 elif day1 == Tuesday and day2 == Monday: return 1 elif day1 == Tuesday and day2 == Tuesday: return 0 # and so onbut I had the notion (is that the right term?) that this probably is not the most efficient solution...
Then I came up with using a list, maybe like:
def sortdays(day1, day2): dayslist = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] if dayslist.index(day1) < dayslist.index(day2): return -1 elif if dayslist.index(day1) == dayslist.index(day2): return 0 else: return 1But it seems not necessary that the results are only -1, 0 and 1? The subtraction will give results from -6 to 6?