Skip to content

Implement in place sorting of OrderMap and OrderSet #55

Closed
@vitiral

Description

@vitiral

Feature request: Add in-place sorting methods to OrderMap (and OrderSet). Similar to the existing .sorted_by, these methods can allow the user to sort by either keys or values or both by supplying all of those to the closure.

For example:

fn sort_by<F>(&mut self, cmp: F)
    where F: FnMut(&K, &V, &K, &V) -> Ordering;

Original issue:


Feature request: add sort_keys<K:Ord+Hash>(&mut self) to do an inplace sort.

This library is INCREDIBLE when assertions need to be made because I can do an inplace sort and the diffs look good (when combined with pretty_assertions of course).

I did a naive implementation like this:

pub fn sort_ordermap<K:Ord+Hash, V>(m: &mut OrderMap<K, V>) {
    let mut ordered: Vec<_> = m.drain(..).collect();
    ordered.sort_by(|left, right| left.0.cmp(&right.0));
    m.extend(ordered.drain(..));
}

pub fn sort_orderset<K:Ord+Hash>(m: &mut OrderSet<K>) {
    let mut ordered: Vec<_> = m.drain(..).collect();
    ordered.sort_by(|left, right| left.cmp(&right));
    m.extend(ordered.drain(..));
}

I'm sure a better one can be done with full access to the underlying data structure.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions