Skip to content

Instantly share code, notes, and snippets.

@019ec6e2
Created April 27, 2023 19:48
Show Gist options
  • Save 019ec6e2/b6f373ef33f9c15c8d845ddcf47502d6 to your computer and use it in GitHub Desktop.
Save 019ec6e2/b6f373ef33f9c15c8d845ddcf47502d6 to your computer and use it in GitHub Desktop.
use std::cmp::Ordering;
pub struct SortablePoint<'a>(pub &'a RetrievedPoint);
pub fn get_value(kind: &Kind) -> Option<String> {
match kind {
Kind::NullValue(_) => None,
Kind::DoubleValue(value) => Some(value.to_string()),
Kind::IntegerValue(value) => Some(value.to_string()),
Kind::StringValue(value) => Some(value.clone()),
Kind::BoolValue(value) => Some(value.to_string()),
Kind::StructValue(value) => Some(format!("{:?}", value)),
Kind::ListValue(value) => Some(format!("{:?}", value)),
}
}
impl<'a> Eq for SortablePoint<'a> {}
impl<'a> PartialOrd for SortablePoint<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<'a> Ord for SortablePoint<'a> {
fn cmp(&self, other: &Self) -> Ordering {
let this_id =
get_value_from_payload(&self.0.payload, "msgId").and_then(|id| id.parse::<i32>().ok());
let other_id =
get_value_from_payload(&other.0.payload, "msgId").and_then(|id| id.parse::<i32>().ok());
dbg!(this_id, other_id);
match (this_id, other_id) {
(Some(a), Some(b)) => a.cmp(&b),
(Some(_), None) => Ordering::Greater, // Treat None as smaller than any i32 value
(None, Some(_)) => Ordering::Less, // Treat None as smaller than any i32 value
(None, None) => Ordering::Equal,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment