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

Add convenience functions for sorting #147

Closed
wants to merge 4 commits into from

Conversation

Kimundi
Copy link
Contributor

@Kimundi Kimundi commented Mar 2, 2021

This adds a sort_by_key() function like the one in std, and a sort_by_file_name() function as a convenience for sorting by file name.

My experience with directory walkers in any languages I used so far was that mostly, I either don't care about the order, or I want an order that is deterministic for the same directory content independent from the underlying system. So if I need to sort, its almost always by file name.

This was my initial motivation for adding sort_by_key, to make sort_by(|a, b| a.file_name().cmp(b.file_name())) easier to write as sort_by_key(|e| e.file_name()). However, due to rust-lang/rust#34162, this does not actually compile, so I also added sort_by_file_name() for what I perceive as the common case.

I kept sort_by_key() for consistency with std, and because there are still valid usecases for it, eg sorting by file size.

@sourcec0de
Copy link

sourcec0de commented Mar 22, 2021

Would love to see this merged.
This is awesome 😄

Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Excellent. LGTM. I like these. Thanks!

@BurntSushi
Copy link
Owner

This PR is on crates.io in walkdir 2.3.2.

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.

3 participants