In my application, I have a dictionary (hash map) of objects. Each object has several attributes, including 'uuid', 'name', and 'level'. uuid is the only unique field and serves as the dictionary key. I would like to be able to list these objects based on their name in ascending alphabetical order, and if the names are the same, then by level in ascending numerical order. To do this, I am trying to maintain a list (array) of uuid's (so I don't have to store entire objects), where I can simply iterate through the list, referencing the appropriate object from the uuid at each particular index. So when I add an object to the dictionary, the list must be updated. I am trying to use a binary search algorithm to first find the index of the list whose uuid matches the appropriate name, then matches the appropriate level. My code is getting me close, but still getting semantic errors. Here it is. Can anyone help me adjust this or point me in the right direction? (I have only given pseudocode to simplify understanding)
min = 0
max = numRecords - 1 #numRecords is the number of objects in the dictionary
mid = 0
while max >= min:
mid = (min + max) / 2
if dict[uuid].name() == dict[list[mid]].name():
if dict[uuid].level() == dict[list[mid]].level():
break
elif dict[uuid].level() < dict[list[mid]].level():
if mid != 0 and dict[uuid].name() == dict[list[mid - 1]].name():
max = mid - 1
else:
break
else:
if mid != numRecords-1 and dict[uuid].name() == dict[list[mid+1]].name():
min = mid + 1
else:
break
elif dict[uuid].name() > dict[list[mid]].name():
min = mid + 1
else:
max = mid - 1
list = list[:mid] + [uuid] + list[mid:]