mirror of
https://github.com/nushell/nushell
synced 2025-01-09 03:39:03 +00:00
69effbc9e7
The `config` command uses different kinds of named arguments, which illustrates how it works.
25 lines
733 B
Rust
25 lines
733 B
Rust
use crate::errors::ShellError;
|
|
use crate::prelude::*;
|
|
|
|
pub fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let fields: Result<Vec<_>, _> = args.positional.iter().map(|a| a.as_string()).collect();
|
|
let fields = fields?;
|
|
|
|
let output = args.input.collect::<Vec<_>>();
|
|
|
|
let output = output.map(move |mut vec| {
|
|
vec.sort_by_key(|item| {
|
|
fields
|
|
.iter()
|
|
.map(|f| item.get_data_by_key(f).map(|i| i.copy()))
|
|
.collect::<Vec<Option<Value>>>()
|
|
});
|
|
|
|
vec.into_iter()
|
|
.map(|v| ReturnValue::Value(v.copy()))
|
|
.collect::<VecDeque<_>>()
|
|
.boxed()
|
|
});
|
|
|
|
Ok(output.flatten_stream().boxed())
|
|
}
|