diff --git a/agent.py b/agent.py index 92175c7..40ec562 100644 --- a/agent.py +++ b/agent.py @@ -318,6 +318,10 @@ def _validate_comparison_request( return None +def _rank_sort_key(row: dict[str, Any]) -> tuple[Any, ...]: + return (-row["score"], row["total_price"], row["delivery_days"], -row["warranty_months"], row["vendor"]) + + def _score_quotes(comparison_id: str, quotes: list[QuoteInput], weights: QuoteWeights) -> dict[str, Any]: total_weight = Decimal(str(weights.price + weights.delivery + weights.warranty)) prices = [_money(q.unit_price) for q in quotes] @@ -351,7 +355,7 @@ def _score_quotes(comparison_id: str, quotes: list[QuoteInput], weights: QuoteWe }, } ) - rows.sort(key=lambda item: (-item["score"], item["total_price"], item["delivery_days"], -item["warranty_months"], item["vendor"])) + rows.sort(key=_rank_sort_key) for rank, row in enumerate(rows, start=1): row["rank"] = rank recommendation = rows[0]