Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[selection] add commands to sort selected rows #2295

Merged
merged 6 commits into from
May 16, 2024

Conversation

midichef
Copy link
Contributor

@midichef midichef commented Feb 4, 2024

This is a proof-of-concept command that sorts only the rows in the selection. @daviewales requested such a feature and I wanted it too.

I recommend using the c79ce4f commit for testing. It uses a temporary workaround for #2266 to allow undo. Otherwise you can't undo the sort. And it uses assertions to detect if rows are lost.

If you think this is worth including, I would appreciate a close inspection of this, @saulpw and @anjakefala , as my first few versions of this caused data corruption.

The practical use of it is a bit limited. Right now you can use sort-selected-asc and -desc to sort by the current column. But if you want to sort by multiple columns you'll have to do it by hand, via exec-python and then
sortSelected(ordering). ordering is a list of (column, boolean) tuples. The boolean is a direction; False means ascending, True means descending. For example, sortSelected([(row1, False), ('title2', True), ('title3'), False)]).

I can imagine a way to reorder rows more flexibly. The user could pull the selection into a new sheet with ", reorder the sheets using keys like [ and z[, or even by sliding rows up and down. And then use a new command to replace the source row selection with the reordered rows. What do you think of that?

Copy link
Owner

@saulpw saulpw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these commands should go in their own file, either features/sort_selected.py or experimental/sort_selected.py. I'm leaning towards the latter, because while the code looks right to me, the idea in general makes me nervous--what if the user aborts this thread with Ctrl+C after the sort but before the replacement? It feels like these should be opt-in, use-at-your-own-risk commands, for the time being at least.

I made the other changes to the sort API to support this, and because they feel better in general.

saulpw added a commit that referenced this pull request Feb 23, 2024
saulpw added a commit that referenced this pull request Feb 23, 2024
saulpw added a commit that referenced this pull request Feb 23, 2024
saulpw added a commit that referenced this pull request Feb 23, 2024
@midichef
Copy link
Contributor Author

midichef commented Mar 1, 2024

Good points about cancelling being dangerous! I hadn't considered that.

We could bypass this if we could edit a copy of the rows and then assign it to self.rows. But Sheet.sort() says we must not assign to self.rows. What are the specific reasons for that?

# must not reassign self.rows: use .sort() instead of sorted()

@midichef midichef marked this pull request as ready for review March 1, 2024 09:25
@saulpw
Copy link
Owner

saulpw commented Mar 1, 2024

We could bypass this if we could edit a copy of the rows and then assign it to self.rows. But Sheet.sort() says we must not assign to self.rows. What are the specific reasons for that?

With git log -S reassign I got to 3930ad0:

by not sorting in-place, sorting on columns sheet would not sort columns on source sheet

It's an edge case for sure, but one that I ran across and felt important to make work for internal design consistency.

@midichef
Copy link
Contributor Author

midichef commented Mar 4, 2024

Oh, I see. Assigning to self.rows on a ColumnsSheet would break the link to the source sheet's columns. It would break sorting in that command. And afterward commands that edit rows, like slide-up, would have no effect on the source sheet. Even cursorRowIndex would give misleading answers that don't correspond to the source sheet columns.

I'll search the git logs more in the future, that note is still helping, 5 years later.

@saulpw saulpw merged commit 6e9bcf0 into saulpw:develop May 16, 2024
10 of 13 checks passed
@midichef midichef deleted the selection_sort branch May 16, 2024 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants