def replace_cells(self, key, sorted_row_idxs):
        row, col, tab = key
        new_keys = {}
        del_keys = []
        selection = self.grid.actions.get_selection()
        for __row, __col, __tab in self.grid.code_array:
            if __tab == tab and \
               (not selection or (__row, __col) in selection):
                new_row = sorted_row_idxs.index(__row)
                if __row != new_row:
                    new_keys[(new_row, __col, __tab)] = \
                        self.grid.code_array((__row, __col, __tab))
                    del_keys.append((__row, __col, __tab))
        for key in del_keys:
            self.grid.code_array.pop(key)
        for key in new_keys:
            CellActions.set_code(self, key, new_keys[key])