Algorithm - managing order of an array

I'm looking for a solution for this problem.

I have an array which defines the rule of the order of elements like below.

let rule = [A,B,C,D,E,F,G,H,I,J,K]

I then have another array whose element can be removed or added back. So for example, I have a list like this:

var list = [A,D,E,I,J,K]

Now If I want to add element 'B' to 'list' the list should be

var list = [A,B,D,E,I,J,K]

because 'B' comes after 'A' and before 'D' in the rule array. So the insertion index would be 1 in this case.

The item in the array are not comparable each other (Let's say a developer can change the order of rule list at any time if that make sense). And there needs no duplicates in the array.

I'm not sure if I explained the problem clearly, but I'd like to know a good approach that finds an insertion index.


Explained the Python code in comments. Basically, find the right place to insert the new element using binary search. The order of elements is decided using rank. The below code assumes that if elements is non-empty then the rule is followed by items in the elements.

rule = ['A','B','C','D','E','F','G','H','I','J','K']
rank = dict()
for i in range(len(rule)):
    rank[rule[i]] = i

elements = ['A','D','E','I','J','K'] #list in which we wish to add elements
target = 'B'    #element to be inserted

#Binary search to find the right place to insert the target in elements
left, right = 0, len(elements)
while left < right:
    mid = left + (right - left) // 2
    if rank[elements[mid]] >= rank[target]:
        right = mid
    else:
        left = mid + 1

elements.insert(left, target) #left is the insertion index
print(elements)

Time complexity of add: O(log(len(elements))) 
Space complexity: O(1)