mirror of
https://github.com/nushell/nushell
synced 2024-11-15 01:17:07 +00:00
sort_by: support -r flag for reverse (#3025)
* sort_by: support -r flag for reverse * Update sort_by.rs Fix reverse test Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
parent
d883ab250a
commit
233161d56e
1 changed files with 45 additions and 4 deletions
|
@ -12,6 +12,7 @@ pub struct SortBy;
|
|||
pub struct SortByArgs {
|
||||
rest: Vec<Tagged<String>>,
|
||||
insensitive: bool,
|
||||
reverse: bool,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
@ -24,9 +25,10 @@ impl WholeStreamCommand for SortBy {
|
|||
Signature::build("sort-by")
|
||||
.switch(
|
||||
"insensitive",
|
||||
"Sort string-based columns case insensitively",
|
||||
"Sort string-based columns case-insensitively",
|
||||
Some('i'),
|
||||
)
|
||||
.switch("reverse", "Sort in reverse order", Some('r'))
|
||||
.rest(SyntaxShape::String, "the column(s) to sort by")
|
||||
}
|
||||
|
||||
|
@ -50,6 +52,16 @@ impl WholeStreamCommand for SortBy {
|
|||
UntaggedValue::int(4).into(),
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
description: "Sort list by decreasing value",
|
||||
example: "echo [2 3 4 1] | sort-by -r",
|
||||
result: Some(vec![
|
||||
UntaggedValue::int(4).into(),
|
||||
UntaggedValue::int(3).into(),
|
||||
UntaggedValue::int(2).into(),
|
||||
UntaggedValue::int(1).into(),
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
description: "Sort output by increasing file size",
|
||||
example: "ls | sort-by size",
|
||||
|
@ -61,7 +73,7 @@ impl WholeStreamCommand for SortBy {
|
|||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Sort strings (case sensitive)",
|
||||
description: "Sort strings (case-sensitive)",
|
||||
example: "echo [airplane Truck Car] | sort-by",
|
||||
result: Some(vec![
|
||||
UntaggedValue::string("Car").into(),
|
||||
|
@ -70,7 +82,16 @@ impl WholeStreamCommand for SortBy {
|
|||
]),
|
||||
},
|
||||
Example {
|
||||
description: "Sort strings (case insensitive)",
|
||||
description: "Sort strings (reversed case-sensitive)",
|
||||
example: "echo [airplane Truck Car] | sort-by -r",
|
||||
result: Some(vec![
|
||||
UntaggedValue::string("airplane").into(),
|
||||
UntaggedValue::string("Truck").into(),
|
||||
UntaggedValue::string("Car").into(),
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
description: "Sort strings (case-insensitive)",
|
||||
example: "echo [airplane Truck Car] | sort-by -i",
|
||||
result: Some(vec![
|
||||
UntaggedValue::string("airplane").into(),
|
||||
|
@ -78,6 +99,15 @@ impl WholeStreamCommand for SortBy {
|
|||
UntaggedValue::string("Truck").into(),
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
description: "Sort strings (reversed case-insensitive)",
|
||||
example: "echo [airplane Truck Car] | sort-by -i -r",
|
||||
result: Some(vec![
|
||||
UntaggedValue::string("Truck").into(),
|
||||
UntaggedValue::string("Car").into(),
|
||||
UntaggedValue::string("airplane").into(),
|
||||
]),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -85,11 +115,22 @@ impl WholeStreamCommand for SortBy {
|
|||
async fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
|
||||
let (SortByArgs { rest, insensitive }, mut input) = args.process().await?;
|
||||
let (
|
||||
SortByArgs {
|
||||
rest,
|
||||
insensitive,
|
||||
reverse,
|
||||
},
|
||||
mut input,
|
||||
) = args.process().await?;
|
||||
let mut vec = input.drain_vec().await;
|
||||
|
||||
sort(&mut vec, &rest, &tag, insensitive)?;
|
||||
|
||||
if reverse {
|
||||
vec.reverse()
|
||||
}
|
||||
|
||||
Ok(futures::stream::iter(vec.into_iter()).to_output_stream())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue