I have a list of elements, with associated sorting keys
data = [
{'data': 0},
{'key': 'foo', 'data': 1},
{'key': 'bar', 'data': 2},
{'data': 3},
{'key': 'foo', 'data': 4},
{'key': 'bar', 'data': 5},
{'key': 'bar', 'data': 6},
{'data': 7}
]
Some elements has sorting key, some does not. I have an external table with sorting for some keys:
ORDER = ['bar', 'foo', 'baz']
Expected result:
[
{'data': 0},
{'key': 'bar', 'data': 2},
{'key': 'bar', 'data': 5},
{'data': 3},
{'key': 'bar', 'data': 6},
{'key': 'foo', 'data': 1},
{'key': 'foo', 'data': 4},
{'data': 7}
]
I need to sort the array in the way that only elements with key are moved, and elements with the same key are not moved. (Basically, I have a partial order for elements with requirements to not to swap elements if key comparison does not require so). As far as I can see, existing list.sort/sorted (in Python) does not support this type of ordering.
I strongly suspect this problem was solved many times, but I can't find a proper name to search.
How this type of sorting is called?