qbuffer
qbuffer

Reputation: 413

Sorting a list of users by datetime with missing dates in Python

I have a list of users with unsorted corresponding datetimes. However, some users in the list do not have a datetime.

This list is in this format:

user1   04-24 19:01:00
user2   04-25 06:39:59
user3
user4   04-24 22:11:34
user5
user6
user7   04-26 10:27:36
user8   04-25 06:09:01

And I want to sort the list like this:

user1   04-24 19:01:00
user4   04-24 22:11:34
user8   04-25 06:09:01
user2   04-25 06:39:59
user3
user5
user6
user7   04-26 10:27:36

Essentially, keep the blank dates in the same position as much as possible (i.e. if an existing date is under a blank date, it cannot go 'up' unless a date greater than it's date exists above the blank one.)

The datetimes are in a modified ISO 8601 format.

If I had all the datetimes, I could sort the list like this:

user_lst.sort(key=lambda date: datetime.strptime(date[len(date)-14:], '%m-%d %H:%M:%S'))

However, since I have some lines without datetimes, I don't know how I'd be able to format it in the way I want to.

Upvotes: 1

Views: 349

Answers (1)

Vasilis G.
Vasilis G.

Reputation: 7844

In this case, you can ignore all elements that have '' as a date and just compare the rest of them. If an element has more recent date than another, then insert it right before the other element and remove it completely from its old position. In every turn you should check if the list in sorted and if it is, then break. The code below demonstrates that concept:

import time

# Print list.
def printList(inList):
    for elem in inList:
        print(elem[0] + ' ' + elem[1])
    print()

# Check if list is sorted.
def isSorted(inList):
    length = len(inList)
    for i in range(length-1):
        if inList[i][1] != '' and inList[i+1][1] != '':
            date1 = time.strptime(inList[i][1], '%m-%d %H:%M:%S')
            date2 = time.strptime(inList[i+1][1], '%m-%d %H:%M:%S')
            if date1 > date2:
                return False
    return True

# Sort list.
def sortList(inList):
    length = len(inList)
    for i in range(length):
        if inList[i][1] == '':
            continue
        for j in range(length-1,0,-1):
            if inList[j][1] == '':
                continue
            if i != j:
                date1 = time.strptime(inList[i][1], '%m-%d %H:%M:%S')
                date2 = time.strptime(inList[j][1], '%m-%d %H:%M:%S')
                if date2 < date1:
                    currentElem = inList[j]
                    inList.remove(inList[j]) 
                    inList.insert(i,currentElem)
        if isSorted(inList):
            break
    return inList


inList = [['user1', '04-24 19:01:00'], ['user2', '04-25 06:39:59'], ['user3', ''], ['user4', '04-24 22:11:34'], ['user5', ''], ['user6', ''], ['user7', '04-26 10:27:36'], ['user8', '04-25 06:09:01']]
inList = sortList(inList)
printList(inList)

Output:

user1 04-24 19:01:00
user4 04-24 22:11:34
user8 04-25 06:09:01
user2 04-25 06:39:59
user3 
user5 
user6 
user7 04-26 10:27:36

Upvotes: 1

Related Questions