diff --git a/src/commands/sort_by.rs b/src/commands/sort_by.rs index f11ff36927..7f6c6a51af 100644 --- a/src/commands/sort_by.rs +++ b/src/commands/sort_by.rs @@ -1,7 +1,7 @@ use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, Value}; +use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use nu_value_ext::get_data_by_key; @@ -41,12 +41,26 @@ fn sort_by( Ok(OutputStream::new(async_stream! { let mut vec = context.input.drain_vec().await; - let calc_key = |item: &Value| { - rest.iter() - .map(|f| get_data_by_key(item, f.borrow_spanned()).map(|i| i.clone())) - .collect::>>() + if vec.is_empty() { + return; + } + + match &vec[0] { + Value { + value: UntaggedValue::Primitive(_), + .. + } => { + vec.sort(); + }, + _ => { + let calc_key = |item: &Value| { + rest.iter() + .map(|f| get_data_by_key(item, f.borrow_spanned())) + .collect::>>() + }; + vec.sort_by_cached_key(calc_key); + }, }; - vec.sort_by_cached_key(calc_key); for item in vec { yield item.into(); diff --git a/tests/commands/sort_by.rs b/tests/commands/sort_by.rs index 4c39b14521..56ea84bcfa 100644 --- a/tests/commands/sort_by.rs +++ b/tests/commands/sort_by.rs @@ -21,3 +21,21 @@ fn by_column() { assert_eq!(actual, "description"); } + +#[test] +fn sort_primitive_values() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + open cargo_sample.toml --raw + | lines + | skip 1 + | first 6 + | sort-by + | first 1 + | echo $it + "# + )); + + assert_eq!(actual, "authors = [\"Yehuda Katz \"]"); +}