Skip to content

Commit

Permalink
Merge pull request #629 from epage/sort
Browse files Browse the repository at this point in the history
feat(edit): Allow sorting Arrays
  • Loading branch information
epage authored Oct 23, 2023
2 parents a76cc6d + a9f1a44 commit 2288fcc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/toml_edit/src/array.rs
Original file line number Diff line number Diff line change
@@ -317,6 +317,26 @@ impl Array {
.retain(|item| item.as_value().map(&mut keep).unwrap_or(false));
}

/// Sorts the array with a key extraction function.
///
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*m* \* *n* \* log(*n*))
/// worst-case, where the key function is *O*(*m*).
#[inline]
pub fn sort_by_key<K, F>(&mut self, mut f: F)
where
F: FnMut(&Value) -> K,
K: Ord,
{
#[allow(clippy::manual_map)] // needed for lifetimes
self.values.sort_by_key(move |item| {
if let Some(value) = item.as_value() {
Some(f(value))
} else {
None
}
});
}

fn value_op<T>(
&mut self,
v: Value,

0 comments on commit 2288fcc

Please sign in to comment.