I’m working with a custom object that needs sorting, but it’s not a regular Python list. The object has indexed elements with sortable values and includes a swap() method for moving elements around.
The problem is that Python’s built-in sort functions expect to work with lists where you can do direct assignment like data[x], data[y] = data[y], data[x]. My object doesn’t support this kind of assignment.
Is there a way to make Python’s sorting work with my custom swap method? I was thinking about creating a wrapper that tracks all the swap operations needed and then applies them to my object, but I’m not sure which methods to override.
Basically, I want to capture all the index pairs that would normally be swapped during sorting so I can call my_object.swap(x, y) for each pair.
Here’s a working example I put together using a custom quicksort:
def divide_array(data, start, finish):
pivot_pos = start
for index in range(start + 1, finish + 1):
if data[index] <= data[start]:
pivot_pos += 1
data.swap(index, pivot_pos)
data.swap(pivot_pos, start)
return pivot_pos
def sort_with_swaps(data, start=0, finish=None):
if finish is None:
finish = len(data) - 1
def _sort_recursive(data, start, finish):
if start >= finish:
return
pivot = divide_array(data, start, finish)
_sort_recursive(data, start, pivot - 1)
_sort_recursive(data, pivot + 1, finish)
return _sort_recursive(data, start, finish)
class CustomSorter(list):
def __init__(self, values, target_object):
super().__init__(values)
self._target = target_object
def swap(self, pos1, pos2):
if pos1 == pos2:
return
self[pos1], self[pos2] = self[pos2], self[pos1]
self._target.swap(pos1, pos2)
sorter = CustomSorter(my_values, my_object)
sort_with_swaps(sorter)
This works fine, but I’m wondering if there’s a way to use Python’s built-in sorting instead of writing my own algorithm. Is this the standard approach or am I missing something?