mirror of
https://github.com/nushell/nushell
synced 2025-01-13 13:49:21 +00:00
Add support for primitive values to sort-by (#1241)
* Remove redundant clone * Add support for primitive values to sort-by #1238
This commit is contained in:
parent
47d987d37f
commit
e059c74a06
2 changed files with 38 additions and 6 deletions
|
@ -1,7 +1,7 @@
|
||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Signature, SyntaxShape, Value};
|
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use nu_value_ext::get_data_by_key;
|
use nu_value_ext::get_data_by_key;
|
||||||
|
|
||||||
|
@ -41,12 +41,26 @@ fn sort_by(
|
||||||
Ok(OutputStream::new(async_stream! {
|
Ok(OutputStream::new(async_stream! {
|
||||||
let mut vec = context.input.drain_vec().await;
|
let mut vec = context.input.drain_vec().await;
|
||||||
|
|
||||||
let calc_key = |item: &Value| {
|
if vec.is_empty() {
|
||||||
rest.iter()
|
return;
|
||||||
.map(|f| get_data_by_key(item, f.borrow_spanned()).map(|i| i.clone()))
|
}
|
||||||
.collect::<Vec<Option<Value>>>()
|
|
||||||
|
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<Option<Value>>>()
|
||||||
|
};
|
||||||
|
vec.sort_by_cached_key(calc_key);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
vec.sort_by_cached_key(calc_key);
|
|
||||||
|
|
||||||
for item in vec {
|
for item in vec {
|
||||||
yield item.into();
|
yield item.into();
|
||||||
|
|
|
@ -21,3 +21,21 @@ fn by_column() {
|
||||||
|
|
||||||
assert_eq!(actual, "description");
|
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 <wycats@gmail.com>\"]");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue