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

Trash correct vertices by changing sort to be numeric-aware #943

Merged
merged 1 commit into from Dec 18, 2019

Conversation

kkaefer
Copy link
Contributor

@kkaefer kkaefer commented Dec 18, 2019

Fixes #897 by correctly sorting coordinate paths prior to deletion.

We're using "coordinate paths" to store which vertices are selected. These paths are stringified indices into the coordinates array, and can be multiple levels deep separated by .. When deleting vertices, we sort them in reverse order to make sure that we invalidate the indices by deleting vertices that come before another vertices scheduled for deletion. This works if we have fewer than 10 vertices. However, the alphabetical sort means that we'd delete note 10 before node 9. This PR fixes this by using a numeric sort.

Copy link
Member

@mourner mourner left a comment

Choose a reason for hiding this comment

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

👍 I wasn't aware of this usage of localeCompare — perhaps it would be simpler to coerce to numbers for sorting, but leaving up to you whether to accept the suggestion.

// Uses number-aware sorting to make sure '9' < '10'. Comparison is reversed because we want them
// in reverse order so that we can remove by index safely.
state.selectedCoordPaths
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
.sort((a, b) => +b - a)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't work for sort keys that are deeply nested, e.g. 0.8.9 should be sorted before 0.8.10

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested that this works with localeCompare's numeric sort:

console.warn(['11', '10', '9', '33', '8', '30', '3'].sort((a, b) => b.localeCompare(a, 'en', { numeric: true })));
console.warn(['4.11', '4.10', '40.8', '4.9', '4.33',].sort((a, b) => b.localeCompare(a, 'en', { numeric: true })));
console.warn(['4.10.34', '4.10.3', '4.10.29'].sort((a, b) => b.localeCompare(a, 'en', { numeric: true })));

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I didn't realize those weren't just numbers, which wasn't clear from the comment. 👍

@kkaefer kkaefer merged commit 29dd32e into master Dec 18, 2019
@kkaefer kkaefer deleted the trash-correct-vertices branch December 18, 2019 15:12
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.

Deleting Vertices - Wrong Points Removed on Trash
2 participants