2019-05-17 17:55:50 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
pub fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-06-22 05:43:37 +02:00
|
|
|
let fields: Result<Vec<_>, _> = args.positional_iter().map(|a| a.as_string()).collect();
|
2019-05-22 09:12:03 +02:00
|
|
|
let fields = fields?;
|
2019-05-17 17:55:50 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
let output = args.input.collect::<Vec<_>>();
|
2019-05-17 17:55:50 +02:00
|
|
|
|
2019-05-23 09:23:06 +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-23 09:23:06 +02:00
|
|
|
});
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
vec.into_iter()
|
|
|
|
.map(|v| ReturnValue::Value(v.copy()))
|
|
|
|
.collect::<VecDeque<_>>()
|
|
|
|
.boxed()
|
|
|
|
});
|
2019-05-17 17:55:50 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
Ok(output.flatten_stream().boxed())
|
2019-05-17 17:55:50 +02:00
|
|
|
}
|