Lets say I want to sort a list that looks like this:
arr = ['45621', '78124', '24613']
The above list stores the IDs for various employees at a company. I don't want to sort based on the IDs alone, but based on attributes that correspond to the IDs, using the following dictionary:
employees = {
'45621' : { 'rating' : 3, 'hours_worked' : 42 },
'78124' : { 'rating' : 4, 'hours_worked' : 78 },
'24613' : { 'rating' : 3, 'hours_worked' : 51 }
}
So its something like this: if an employee has a higher rating
, his/her ID will come first. However, if 2 employees have the same rating
, then we compare the hours_worked
, and whoever has worked more will come before the other.
Right now, I am thinking about 2 different sorting methods: insertion, and merge. I edited a few code samples from the web, but I am struggling to compare the second condition, that is, when 2 ratings are equal for the algorithms. For instance, the edited versions of my insertion sort looks like this:
InsertionSort
def insertionSort(arr):
for i in range(1, len(arr)):
key = employees[ arr[i] ]['rating']
j = i-1
# Falls apart after this part
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
The merge sort seems even more complex, but I am trying to at least understand one to get an idea.
Any help with these sorting methods will be greatly appreciated. Thanks.
Note: I don't want to use a built in sorting mechanism, as this is mainly for learning, so it is not a duplicate.