Apply sorting to a list of objects using dot notation for nested attributes.
(objects: List[Any], sort_by: str, sort_order: str)
| 109 | |
| 110 | |
| 111 | def apply_sorting(objects: List[Any], sort_by: str, sort_order: str) -> List[Any]: |
| 112 | """Apply sorting to a list of objects using dot notation for nested attributes.""" |
| 113 | |
| 114 | def get_nested_attr(obj, attr_path: str): |
| 115 | """Get nested attribute using dot notation.""" |
| 116 | attrs = attr_path.split(".") |
| 117 | current = obj |
| 118 | for attr in attrs: |
| 119 | if hasattr(current, attr): |
| 120 | current = getattr(current, attr) |
| 121 | elif isinstance(current, dict) and attr in current: |
| 122 | current = current[attr] |
| 123 | else: |
| 124 | return None |
| 125 | return current |
| 126 | |
| 127 | def sort_key(obj): |
| 128 | value = get_nested_attr(obj, sort_by) |
| 129 | if value is None: |
| 130 | return ("", 0, None) |
| 131 | |
| 132 | if isinstance(value, str): |
| 133 | return (value.lower(), 1, None) |
| 134 | elif isinstance(value, (int, float)): |
| 135 | return ("", 1, value) |
| 136 | else: |
| 137 | return (str(value).lower(), 1, None) |
| 138 | |
| 139 | reverse = sort_order.lower() == "desc" |
| 140 | |
| 141 | try: |
| 142 | return sorted(objects, key=sort_key, reverse=reverse) |
| 143 | except Exception as e: |
| 144 | logger.warning(f"Failed to sort objects by '{sort_by}': {e}") |
| 145 | return objects |
| 146 | |
| 147 | |
| 148 | def _build_any_feature_view_proto(feature_view: BaseFeatureView): |
no outgoing calls
no test coverage detected