BurntSushi / walkdir

Rust library for walking directories recursively.
The Unlicense
1.27k stars 109 forks source link

Add convenience functions for sorting #147

Closed Kimundi closed 3 years ago

Kimundi commented 3 years ago

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 https://github.com/rust-lang/rust/issues/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 commented 3 years ago

Would love to see this merged. This is awesome 😄

BurntSushi commented 3 years ago

This PR is on crates.io in walkdir 2.3.2.