Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists.sort() with cmp
#8
(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 list4
only 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 on
but 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 1
But it seems not necessary that the results are only -1, 0 and 1? The subtraction will give results from -6 to 6?
Reply


Messages In This Thread
lists.sort() with cmp - by merlem - Feb-06-2017, 04:47 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 05:26 PM
RE: lists.sort() with cmp - by merlem - Feb-06-2017, 06:01 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 06:14 PM
RE: lists.sort() with cmp - by merlem - Feb-06-2017, 08:09 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-06-2017, 08:32 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-06-2017, 06:40 PM
RE: lists.sort() with cmp - by wavic - Feb-06-2017, 06:41 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 07:14 PM
RE: lists.sort() with cmp - by nilamo - Feb-08-2017, 07:29 AM
RE: lists.sort() with cmp - by merlem - Feb-09-2017, 02:05 PM
RE: lists.sort() with cmp - by wavic - Feb-09-2017, 02:16 PM
RE: lists.sort() with cmp - by merlem - Feb-09-2017, 03:29 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-09-2017, 09:12 PM
RE: lists.sort() with cmp - by snippsat - Feb-09-2017, 09:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,427 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 14,623 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,572 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,485 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Creating new list from 2 lists after a custom sort pythoneer 12 6,313 Jun-01-2018, 04:55 PM
Last Post: pythoneer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020