2019-05-17 17:55:50 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
pub fn sort_by(args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
let fields: Result<Vec<_>, _> = args.args.iter().map(|a| a.as_string()).collect();
|
|
|
|
let fields = fields?;
|
2019-05-17 17:55:50 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
let mut output = args.input.into_iter().collect::<Vec<_>>();
|
2019-05-17 17:55:50 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
output.sort_by_key(|item| {
|
|
|
|
fields
|
2019-05-17 17:55:50 +02:00
|
|
|
.iter()
|
2019-05-22 09:12:03 +02:00
|
|
|
.map(|f| item.get_data_by_key(f).borrow().copy())
|
|
|
|
.collect::<Vec<Value>>()
|
|
|
|
});
|
|
|
|
|
|
|
|
let output = output
|
|
|
|
.iter()
|
|
|
|
.map(|o| ReturnValue::Value(o.copy()))
|
|
|
|
.collect();
|
2019-05-17 17:55:50 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
Ok(output)
|
2019-05-17 17:55:50 +02:00
|
|
|
}
|