You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "uranusjr (via GitHub)" <gi...@apache.org> on 2023/06/28 06:35:19 UTC

[GitHub] [airflow] uranusjr commented on a diff in pull request #32179: Allow to sort Grid View alphabetically

uranusjr commented on code in PR #32179:
URL: https://github.com/apache/airflow/pull/32179#discussion_r1244748654


##########
airflow/utils/task_group.py:
##########
@@ -425,6 +426,29 @@ def serialize_for_task_group(self) -> tuple[DagAttributeTypes, Any]:
 
         return DagAttributeTypes.TASK_GROUP, TaskGroupSerialization.serialize_task_group(self)
 
+    def hierarchical_alphabetical_sort(self):
+        """
+        Sorts children in hierarchical alphabetical order:
+        - groups in alphabetical order first
+        - tasks in alphabetical order after them.
+
+        :return: list of tasks in hierarchical alphabetical order
+        """
+
+        def compare_str(a: str, b: str) -> int:
+            return (a > b) - (a < b)
+
+        def compare(a: DAGNode, b: DAGNode) -> int:
+            is_a_group = isinstance(a, TaskGroup)
+            is_b_group = isinstance(b, TaskGroup)
+            if is_a_group == is_b_group:
+                return compare_str(a.node_id, b.node_id)
+            if is_a_group:
+                return -1
+            return 1
+
+        return sorted(self.children.values(), key=cmp_to_key(compare))

Review Comment:
   Why not implement a key function? `cmp_to_key` is mainly used for the ease of porting code that was written before key functions were introduced (somewhere like Python 2.5 iirc).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org