sort_by: coerce_compare now returns an Ordering (#4461)

* coerce_compare now returns an Ordering which will enable mixed type comparison

* arbitrary nushell sort order of Float / Int / String / Bool
This commit is contained in:
Michael Angerman 2022-02-13 09:23:54 -08:00 committed by GitHub
parent 3576350b4b
commit 7a3aeaf080
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -183,7 +183,6 @@ pub fn sort(
config, config,
) )
.expect("sort_by Value::Record bug") .expect("sort_by Value::Record bug")
.compare()
}); });
} }
_ => { _ => {
@ -199,11 +198,8 @@ pub fn sort(
); );
coerce_compare(&lowercase_left, &lowercase_right, call) coerce_compare(&lowercase_left, &lowercase_right, call)
.expect("sort_by default bug") .expect("sort_by default bug")
.compare()
} else { } else {
coerce_compare(a, b, call) coerce_compare(a, b, call).expect("sort_by default bug")
.expect("sort_by default bug")
.compare()
} }
}); });
} }
@ -218,7 +214,7 @@ pub fn process(
call: &Call, call: &Call,
insensitive: bool, insensitive: bool,
config: &Config, config: &Config,
) -> Result<CompareValues, ShellError> { ) -> Result<Ordering, ShellError> {
let left_value = left.get_data_by_key(column); let left_value = left.get_data_by_key(column);
let left_res = match left_value { let left_res = match left_value {
@ -280,36 +276,64 @@ pub fn process_floats(left: &f64, right: &f64) -> std::cmp::Ordering {
} }
} }
pub fn coerce_compare( /*
left: &Value, Arbitrary Order of Values:
right: &Value, Floats
call: &Call, Ints
) -> Result<CompareValues, ShellError> { Strings
match (left, right) { Bools
*/
pub fn coerce_compare(left: &Value, right: &Value, call: &Call) -> Result<Ordering, ShellError> {
Ok(match (left, right) {
(Value::Float { val: left, .. }, Value::Float { val: right, .. }) => { (Value::Float { val: left, .. }, Value::Float { val: right, .. }) => {
Ok(CompareValues::Floats(*left, *right)) CompareValues::Floats(*left, *right).compare()
} }
(Value::Filesize { val: left, .. }, Value::Filesize { val: right, .. }) => { (Value::Filesize { val: left, .. }, Value::Filesize { val: right, .. }) => {
Ok(CompareValues::Filesize(*left, *right)) CompareValues::Filesize(*left, *right).compare()
} }
(Value::Date { val: left, .. }, Value::Date { val: right, .. }) => { (Value::Date { val: left, .. }, Value::Date { val: right, .. }) => {
Ok(CompareValues::Date(*left, *right)) CompareValues::Date(*left, *right).compare()
} }
(Value::Int { val: left, .. }, Value::Int { val: right, .. }) => { (Value::Int { val: left, .. }, Value::Int { val: right, .. }) => {
Ok(CompareValues::Ints(*left, *right)) CompareValues::Ints(*left, *right).compare()
} }
(Value::String { val: left, .. }, Value::String { val: right, .. }) => { (Value::String { val: left, .. }, Value::String { val: right, .. }) => {
Ok(CompareValues::String(left.clone(), right.clone())) CompareValues::String(left.clone(), right.clone()).compare()
} }
(Value::Bool { val: left, .. }, Value::Bool { val: right, .. }) => { (Value::Bool { val: left, .. }, Value::Bool { val: right, .. }) => {
Ok(CompareValues::Booleans(*left, *right)) CompareValues::Booleans(*left, *right).compare()
} }
// Floats will always come before Ints
(Value::Float { .. }, Value::Int { .. }) => Ordering::Less,
(Value::Int { .. }, Value::Float { .. }) => Ordering::Greater,
// Floats will always come before Strings
(Value::Float { .. }, Value::String { .. }) => Ordering::Less,
(Value::String { .. }, Value::Float { .. }) => Ordering::Greater,
// Floats will always come before Bools
(Value::Float { .. }, Value::Bool { .. }) => Ordering::Less,
(Value::Bool { .. }, Value::Float { .. }) => Ordering::Greater,
// Ints will always come before strings
(Value::Int { .. }, Value::String { .. }) => Ordering::Less,
(Value::String { .. }, Value::Int { .. }) => Ordering::Greater,
// Ints will always come before Bools
(Value::Int { .. }, Value::Bool { .. }) => Ordering::Less,
(Value::Bool { .. }, Value::Int { .. }) => Ordering::Greater,
// Strings will always come before Bools
(Value::String { .. }, Value::Bool { .. }) => Ordering::Less,
(Value::Bool { .. }, Value::String { .. }) => Ordering::Greater,
_ => { _ => {
let description = format!("not able to compare {:?} with {:?}\n", left, right); let description = format!("not able to compare {:?} with {:?}\n", left, right);
Err(ShellError::TypeMismatch(description, call.head)) return Err(ShellError::TypeMismatch(description, call.head));
} }
} })
} }
#[cfg(test)] #[cfg(test)]