nushell/src/commands/sort_by.rs

26 lines
727 B
Rust
Raw Normal View History

2019-05-17 17:55:50 +02:00
use crate::errors::ShellError;
use crate::prelude::*;
pub fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-05-22 09:12:03 +02:00
let fields: Result<Vec<_>, _> = args.args.iter().map(|a| a.as_string()).collect();
let fields = fields?;
2019-05-17 17:55:50 +02:00
let output = args.input.collect::<Vec<_>>();
2019-05-17 17:55:50 +02:00
let output = output.map(move |mut vec| {
vec.sort_by_key(|item| {
fields
.iter()
2019-05-28 08:45:18 +02:00
.map(|f| item.get_data_by_key(f).map(|i| i.copy()))
.collect::<Vec<Option<Value>>>()
});
2019-05-22 09:12:03 +02:00
vec.into_iter()
.map(|v| ReturnValue::Value(v.copy()))
.collect::<VecDeque<_>>()
.boxed()
});
2019-05-17 17:55:50 +02:00
Ok(output.flatten_stream().boxed())
2019-05-17 17:55:50 +02:00
}