lakwet / voracious_sort

Voracious radix sort
MIT License
65 stars 2 forks source link

Trait wrappers for lambdas #15

Open LucaCappelletti94 opened 3 months ago

LucaCappelletti94 commented 3 months ago

Hi! In my use case, I need to sort a vector containing both positive and negative values as if they are absolute.

I can do this with the sort_unstable_by quite simply using:

x.sort_unstable_by(|a, b| {
    a.abs()
        .partial_cmp(&b.abs())
        .unwrap_or(std::cmp::Ordering::Equal)
});

Is there a way to do this with your library?

lakwet commented 3 months ago

hello It is not possible to do that with the primitive types (not with the lib in its current version) however it is possible to do that by implementing the trait for a struct documentation here: https://docs.rs/voracious_radix_sort/latest/voracious_radix_sort/ there is a deep difference between a radix sort and a comparative sort, the radix sort does not use a comparative function, thus, it cannot take a lambda (a comparative function) to sort the elements.

LucaCappelletti94 commented 2 months ago

I have prepared a very generic wrapper that works well, I will make a PR so that it is available to other users. Performance-wise, measured with criterion, there seems to be no difference between using the struct and this other generic approach.