Reorder an array according to some function Args: arr (List[Any]): The initial array fn (Callable[[Any], Any]): A function to determine the priority of elements
(self, arr: List[Any], fn: Callable)
| 168 | |
| 169 | class Reorderer: |
| 170 | def __init__(self, arr: List[Any], fn: Callable) -> None: |
| 171 | """Reorder an array according to some function |
| 172 | |
| 173 | Args: |
| 174 | arr (List[Any]): The initial array |
| 175 | fn (Callable[[Any], Any]): A function to determine the priority of elements |
| 176 | """ |
| 177 | self.size = len(arr) |
| 178 | arr = list(enumerate(arr)) |
| 179 | arr = group(arr, lambda x: fn(x[1])) |
| 180 | # arr = [([y[0] for y in x], x[0][1]) for x in arr] |
| 181 | # TODO: overhaul reorderer. It currently grouped requests by content but we don't want this |
| 182 | arr = [([y[0]], x[0][1]) for x in arr for y in x] |
| 183 | arr.sort(key=lambda x: fn(x[1])) |
| 184 | |
| 185 | self.arr = arr |
| 186 | |
| 187 | def get_reordered(self): |
| 188 | """Gets the reordered array |