def _sort_by_metrics(self, items: list[ModelOutput], objective_dict: dict[str, Any]) -> list[ModelOutput]:
        """Sort items based on metrics according to objective direction.

        Args:
            items: List of ModelOutput objects to sort
            objective_dict: Dictionary of objectives with their priorities

        Returns:
            List of ModelOutput objects sorted by their metrics

        """
        if not items:
            return []

        if not objective_dict:
            return items

        try:
            return sorted(
                items,
                key=lambda x: tuple(
                    x.metrics["value"][metric]["value"]
                    if x.metrics["cmp_direction"][metric] == 1
                    else -x.metrics["value"][metric]["value"]
                    for metric in objective_dict
                ),
                reverse=True,
            )
        except (KeyError, TypeError) as e:
            # Handle case where metrics might be missing or incorrectly formatted
            logger.warning("Warning: Error during metric sorting: %s", e)
            return items
Sort items based on metrics according to objective direction.

        Args:
            items: List of ModelOutput objects to sort
            objective_dict: Dictionary of objectives with their priorities

        Returns:
            List of ModelOutput objects sorted by their metrics

        
olive/engine/output.py