mirror of
https://github.com/nushell/nushell
synced 2024-12-28 05:53:09 +00:00
sort not change shape (#5778)
This commit is contained in:
parent
48e29e9ed6
commit
3c0cbec993
1 changed files with 32 additions and 14 deletions
|
@ -1,8 +1,8 @@
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
|
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
|
||||||
Value,
|
Signature, Span, Value,
|
||||||
};
|
};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
@ -95,6 +95,15 @@ impl Command for Sort {
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Sort doesn't change input shape",
|
||||||
|
example: "{a: 3, b: 4} | sort",
|
||||||
|
result: Some(Value::Record {
|
||||||
|
cols: vec!["a".to_string(), "b".to_string()],
|
||||||
|
vals: vec![Value::test_int(3), Value::test_int(4)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,20 +117,29 @@ impl Command for Sort {
|
||||||
let reverse = call.has_flag("reverse");
|
let reverse = call.has_flag("reverse");
|
||||||
let insensitive = call.has_flag("insensitive");
|
let insensitive = call.has_flag("insensitive");
|
||||||
let metadata = &input.metadata();
|
let metadata = &input.metadata();
|
||||||
let mut vec: Vec<_> = input.into_iter().collect();
|
|
||||||
|
|
||||||
sort(&mut vec, call.head, insensitive)?;
|
match input {
|
||||||
|
PipelineData::Value(v, ..)
|
||||||
if reverse {
|
if !matches!(v, Value::List { .. } | Value::Range { .. }) =>
|
||||||
vec.reverse()
|
{
|
||||||
}
|
Ok(v.into_pipeline_data())
|
||||||
|
}
|
||||||
let iter = vec.into_iter();
|
pipe_data => {
|
||||||
match &*metadata {
|
let mut vec: Vec<_> = pipe_data.into_iter().collect();
|
||||||
Some(m) => {
|
|
||||||
Ok(iter.into_pipeline_data_with_metadata(m.clone(), engine_state.ctrlc.clone()))
|
sort(&mut vec, call.head, insensitive)?;
|
||||||
|
|
||||||
|
if reverse {
|
||||||
|
vec.reverse()
|
||||||
|
}
|
||||||
|
|
||||||
|
let iter = vec.into_iter();
|
||||||
|
match &*metadata {
|
||||||
|
Some(m) => Ok(iter
|
||||||
|
.into_pipeline_data_with_metadata(m.clone(), engine_state.ctrlc.clone())),
|
||||||
|
None => Ok(iter.into_pipeline_data(engine_state.ctrlc.clone())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => Ok(iter.into_pipeline_data(engine_state.ctrlc.clone())),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue