Skip to content

Commit 27e39bc

Browse files
committed
refactor: update ordering function
1 parent 1b0ed33 commit 27e39bc

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

webapp/src/ts/reducers/tasks.ts

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,60 @@ const initialState = {
1414
},
1515
};
1616

17-
const orderByDueDate = (t1, t2) => {
18-
const lhs = t1?.dueDate;
19-
const rhs = t2?.dueDate;
20-
if (!lhs && !rhs) {
21-
return 0;
22-
}
23-
if (!lhs) {
24-
return 1;
17+
/**
18+
* Task prioritization algorithm that combines:
19+
* 1. Overdue status (most urgent)
20+
* 2. Due today status (high urgency)
21+
* 3. Priority (importance)
22+
* 4. Due date (soonest first)
23+
* 5. Tasks without due dates (lowest priority)
24+
*
25+
* Sorting rules (in order):
26+
* 1. Overdue tasks appear first (most urgent), sorted by priority (higher first)
27+
* 2. Tasks due today appear next, sorted by priority (higher first)
28+
* 3. Then sort other dates too by date and priority (high to low)
29+
* 4. For equal priority, sort by due date (earlier first)
30+
* 5. Tasks without due dates appear last
31+
*/
32+
const orderByDueDateAndPriority = (t1, t2) => {
33+
const getPriorityScore = (t) => {
34+
if (t?.priority === 'high') {
35+
return 10;
36+
} else if (t?.priority === 'medium') {
37+
return 6;
38+
} else if (typeof t?.priority === 'string') {
39+
return 0;
40+
} else {
41+
return t?.priority ?? 0;
42+
}
43+
};
44+
45+
const now = new Date();
46+
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
47+
48+
const getDateScore = (t) => {
49+
if (!t?.dueDate) return Infinity;
50+
const date = new Date(t.dueDate).getTime();
51+
return Math.floor((date - startOfToday) / (1000 * 60 * 60 * 24));
52+
};
53+
54+
const p1 = getPriorityScore(t1);
55+
const p2 = getPriorityScore(t2);
56+
const days1 = getDateScore(t1);
57+
const days2 = getDateScore(t2);
58+
59+
// Compare by due date (earlier first)
60+
if (days1 !== days2) {
61+
return days1 - days2;
2562
}
26-
if (!rhs) {
27-
return -1;
63+
64+
// Compare by priority (higher first)
65+
if (p1 !== p2) {
66+
return p2 - p1;
2867
}
2968

30-
return lhs < rhs ? -1 : 1;
69+
// Otherwise maintain original order
70+
return 0;
3171
};
3272

3373
const _tasksReducer = createReducer(
@@ -37,7 +77,7 @@ const _tasksReducer = createReducer(
3777
on(Actions.setTasksList, (state, { payload: { tasks } }) => {
3878
return {
3979
...state,
40-
tasksList: [...tasks].sort(orderByDueDate),
80+
tasksList: [...tasks].sort(orderByDueDateAndPriority),
4181
};
4282
}),
4383

0 commit comments

Comments
 (0)