From eed27b79e91f50aae9f14fdda0bcc0019a0b01a0 Mon Sep 17 00:00:00 2001 From: Louis Chan Date: Tue, 7 Jan 2025 17:14:01 +0800 Subject: [PATCH] Add basic property sorting --- raphtory-graphql/src/model/graph/edges.rs | 39 ++++++++++++++--------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/raphtory-graphql/src/model/graph/edges.rs b/raphtory-graphql/src/model/graph/edges.rs index 12cd578ac..e2e880d68 100644 --- a/raphtory-graphql/src/model/graph/edges.rs +++ b/raphtory-graphql/src/model/graph/edges.rs @@ -36,12 +36,12 @@ impl GqlEdges { #[derive(InputObject, Clone, Debug, Eq, PartialEq)] pub struct EdgeSortBy { pub reverse: Option, - pub time: Option, + pub time: Option, pub property: Option, } #[derive(Enum, Clone, Debug, Eq, PartialEq)] -pub enum SortByTime { +pub enum EdgeSortByTime { Latest, Earliest, } @@ -122,26 +122,35 @@ impl GqlEdges { sort_bys .clone() .into_iter() - .fold(Ordering::Equal, |cmp, sort_by| { - cmp.then_with(|| { + .fold(Ordering::Equal, |current_ordering, sort_by| { + current_ordering.then_with(|| { let ordering = if let Some(sort_by_time) = sort_by.time { - match sort_by_time { - SortByTime::Latest => { - first_edge.latest_time().cmp(&second_edge.latest_time()) + let (first_time, second_time) = match sort_by_time { + EdgeSortByTime::Latest => { + (first_edge.latest_time(), second_edge.latest_time()) } - SortByTime::Earliest => { - first_edge.earliest_time().cmp(&second_edge.earliest_time()) + EdgeSortByTime::Earliest => { + (first_edge.earliest_time(), second_edge.earliest_time()) } - } + }; + first_time.partial_cmp(&second_time) } else if let Some(sort_by_property) = sort_by.property { - todo!("To be done in the future") + let first_prop_maybe = + first_edge.properties().get(&*sort_by_property); + let second_prop_maybe = + second_edge.properties().get(&*sort_by_property); + first_prop_maybe.partial_cmp(&second_prop_maybe) } else { - Ordering::Equal + None }; - if sort_by.reverse == Some(true) { - ordering.reverse() + if let Some(ordering) = ordering { + if sort_by.reverse == Some(true) { + ordering.reverse() + } else { + ordering + } } else { - ordering + Ordering::Equal } }) })