Skip to content

Commit

Permalink
feat(edit): Allow sorting Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 23, 2023
1 parent 7cdd43c commit a9f1a44
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
Expand Up @@ -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,
Expand Down

0 comments on commit a9f1a44

Please sign in to comment.