MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / compare_json_values

Function compare_json_values

nodedb/src/data/executor/handlers/aggregate.rs:139–166  ·  view source on GitHub ↗

Compare two `Option<&serde_json::Value>` for sort. Nulls / absent keys sort last; numbers compare numerically; everything else falls back to string comparison.

(
    a: Option<&serde_json::Value>,
    b: Option<&serde_json::Value>,
)

Source from the content-addressed store, hash-verified

137/// keys sort last; numbers compare numerically; everything else falls
138/// back to string comparison.
139fn compare_json_values(
140 a: Option<&serde_json::Value>,
141 b: Option<&serde_json::Value>,
142) -> std::cmp::Ordering {
143 use serde_json::Value as V;
144 use std::cmp::Ordering;
145 let a_is_null = matches!(a, None | Some(V::Null));
146 let b_is_null = matches!(b, None | Some(V::Null));
147 if a_is_null && b_is_null {
148 return Ordering::Equal;
149 }
150 if a_is_null {
151 return Ordering::Greater;
152 }
153 if b_is_null {
154 return Ordering::Less;
155 }
156 match (a.unwrap(), b.unwrap()) {
157 (V::Number(x), V::Number(y)) => {
158 let xf = x.as_f64().unwrap_or(0.0);
159 let yf = y.as_f64().unwrap_or(0.0);
160 xf.partial_cmp(&yf).unwrap_or(Ordering::Equal)
161 }
162 (V::String(x), V::String(y)) => x.cmp(y),
163 (V::Bool(x), V::Bool(y)) => x.cmp(y),
164 (x, y) => x.to_string().cmp(&y.to_string()),
165 }
166}
167
168// ── CoreLoop impl ──────────────────────────────────────────────────────────
169

Callers 1

sort_aggregated_rowsFunction · 0.85

Calls 4

to_stringMethod · 0.80
as_f64Method · 0.45
partial_cmpMethod · 0.45
cmpMethod · 0.45

Tested by

no test coverage detected