Skip to content

Commit

Permalink
refactor: remove cmp_to_key
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Nov 14, 2024
1 parent 0e20272 commit 04de8bb
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions dooit/api/todo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from functools import cmp_to_key
from typing import TYPE_CHECKING, Optional, Union
from datetime import datetime, timedelta
from typing import List
Expand All @@ -12,19 +11,6 @@
from dooit.api.workspace import Workspace


def _custom_sort_by_status(x: "Todo", y: "Todo") -> int:
x_values = [not x.pending, x.due or datetime.max, x.order_index]
y_values = [not y.pending, y.due or datetime.max, y.order_index]

for x_val, y_val in zip(x_values, y_values):
if x_val < y_val:
return -1
elif x_val > y_val:
return 1

return 0


class Todo(DooitModel):
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
order_index: Mapped[int] = mapped_column(default=-1)
Expand Down Expand Up @@ -122,7 +108,11 @@ def sort_siblings(self, field: str):
else:
items = sorted(
self.siblings,
key=cmp_to_key(_custom_sort_by_status),
key=lambda x: (
not x.pending,
x.due or datetime.max,
x.order_index,
),
)

for index, todo in enumerate(items):
Expand Down

0 comments on commit 04de8bb

Please sign in to comment.