mirror of
https://github.com/nushell/nushell.git
synced 2025-05-07 19:44:25 +02:00
add support for Floats for sort-by (#857)
This commit is contained in:
parent
e8b8836977
commit
a4421434d9
@ -7,6 +7,7 @@ use nu_protocol::{
|
|||||||
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
|
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
|
||||||
SyntaxShape, Value,
|
SyntaxShape, Value,
|
||||||
};
|
};
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SortBy;
|
pub struct SortBy;
|
||||||
@ -155,7 +156,7 @@ pub fn process(
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum CompareValues {
|
pub enum CompareValues {
|
||||||
Ints(i64, i64),
|
Ints(i64, i64),
|
||||||
// Floats(f64, f64),
|
Floats(f64, f64),
|
||||||
String(String, String),
|
String(String, String),
|
||||||
Booleans(bool, bool),
|
Booleans(bool, bool),
|
||||||
Filesize(i64, i64),
|
Filesize(i64, i64),
|
||||||
@ -166,8 +167,7 @@ impl CompareValues {
|
|||||||
pub fn compare(&self) -> std::cmp::Ordering {
|
pub fn compare(&self) -> std::cmp::Ordering {
|
||||||
match self {
|
match self {
|
||||||
CompareValues::Ints(left, right) => left.cmp(right),
|
CompareValues::Ints(left, right) => left.cmp(right),
|
||||||
// f64: std::cmp::Ord is required
|
CompareValues::Floats(left, right) => process_floats(left, right),
|
||||||
// CompareValues::Floats(left, right) => left.cmp(right),
|
|
||||||
CompareValues::String(left, right) => left.cmp(right),
|
CompareValues::String(left, right) => left.cmp(right),
|
||||||
CompareValues::Booleans(left, right) => left.cmp(right),
|
CompareValues::Booleans(left, right) => left.cmp(right),
|
||||||
CompareValues::Filesize(left, right) => left.cmp(right),
|
CompareValues::Filesize(left, right) => left.cmp(right),
|
||||||
@ -176,22 +176,29 @@ impl CompareValues {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn process_floats(left: &f64, right: &f64) -> std::cmp::Ordering {
|
||||||
|
let result = left.partial_cmp(right);
|
||||||
|
match result {
|
||||||
|
Some(Ordering::Greater) => Ordering::Greater,
|
||||||
|
Some(Ordering::Less) => Ordering::Less,
|
||||||
|
_ => Ordering::Equal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn coerce_compare(
|
pub fn coerce_compare(
|
||||||
left: &Value,
|
left: &Value,
|
||||||
right: &Value,
|
right: &Value,
|
||||||
) -> Result<CompareValues, (&'static str, &'static str)> {
|
) -> Result<CompareValues, (&'static str, &'static str)> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
// (Value::Float { val: left, .. }, Value::Float { val: right, .. }) => {
|
(Value::Float { val: left, .. }, Value::Float { val: right, .. }) => {
|
||||||
// Ok(CompareValues::Floats(*left, *right))
|
Ok(CompareValues::Floats(*left, *right))
|
||||||
// }
|
}
|
||||||
(Value::Filesize { val: left, .. }, Value::Filesize { val: right, .. }) => {
|
(Value::Filesize { val: left, .. }, Value::Filesize { val: right, .. }) => {
|
||||||
Ok(CompareValues::Filesize(*left, *right))
|
Ok(CompareValues::Filesize(*left, *right))
|
||||||
}
|
}
|
||||||
|
|
||||||
(Value::Date { val: left, .. }, Value::Date { val: right, .. }) => {
|
(Value::Date { val: left, .. }, Value::Date { val: right, .. }) => {
|
||||||
Ok(CompareValues::Date(*left, *right))
|
Ok(CompareValues::Date(*left, *right))
|
||||||
}
|
}
|
||||||
|
|
||||||
(Value::Int { val: left, .. }, Value::Int { val: right, .. }) => {
|
(Value::Int { val: left, .. }, Value::Int { val: right, .. }) => {
|
||||||
Ok(CompareValues::Ints(*left, *right))
|
Ok(CompareValues::Ints(*left, *right))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user