Complete worked example
python
import numpy as np
def inspect_example(identifiers, scores):
ids = np.asarray(identifiers, dtype=np.int64)
values = np.asarray(scores, dtype=np.float64)
if ids.ndim != 1 or ids.shape != values.shape:
raise ValueError("expected aligned vectors")
if np.unique(ids).size != ids.size or not np.isfinite(values).all():
raise ValueError("expected unique identifiers and finite scores")
order = np.argsort(-values, kind="stable")
return {"ids": ids[order].tolist(), "scores": values[order].tolist()}- Reasoning
- The worked function validates equal-length vectors, unique identifiers and finite scores. It sorts the negated scores with kind="stable", then applies the resulting positions to both arrays. A fixture with tied scores and deliberately unordered identifiers makes the stable input-order policy visible. The return boundary converts NumPy scalars to ordinary JSON-compatible lists. Trace one position from the original arrays into the result. Its identifier and score must travel together. This technique extends to several numerical columns, but it still depends on an established canonical row identity before sorting. A score sort is not a deduplication algorithm.