summaryrefslogtreecommitdiff
path: root/src/eval/array.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval/array.rs')
-rw-r--r--src/eval/array.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs
index acf44ab2..bae89c4b 100644
--- a/src/eval/array.rs
+++ b/src/eval/array.rs
@@ -1,3 +1,4 @@
+use std::cmp::Ordering;
use std::convert::TryFrom;
use std::fmt::{self, Debug, Display, Formatter, Write};
use std::iter::FromIterator;
@@ -80,6 +81,26 @@ impl Array {
self.0.iter()
}
+ /// Return a sorted version of this array.
+ ///
+ /// Returns an error if two values could not be compared.
+ pub fn sorted(mut self) -> StrResult<Self> {
+ let mut result = Ok(());
+ Rc::make_mut(&mut self.0).sort_by(|a, b| {
+ a.partial_cmp(b).unwrap_or_else(|| {
+ if result.is_ok() {
+ result = Err(format!(
+ "cannot compare {} with {}",
+ a.type_name(),
+ b.type_name(),
+ ));
+ }
+ Ordering::Equal
+ })
+ });
+ result.map(|_| self)
+ }
+
/// Repeat this array `n` times.
pub fn repeat(&self, n: i64) -> StrResult<Self> {
let count = usize::try_from(n)