Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Sort by time button and feature. #182

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ <h1 class="subtitle">Todo-List</h1>
<button class="sort-button" onclick="sortTodos('priority')">
Sort by Priority
</button>
<button class="sort-button" onclick="sortTodos('time')">
Sort by time
</button>
<select class="js-filter-input" onchange="filterTodos()">
<option value="all">All</option>
<option value="pending">Pending</option>
Expand Down
13 changes: 12 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ function updateTodoList() {
return currentSortOrder === 'asc'
? priorityOrder[a.priority] - priorityOrder[b.priority]
: priorityOrder[b.priority] - priorityOrder[a.priority];
} else if (currentSortMethod === 'time') {
// Directly compare time strings
return currentSortOrder === 'asc'
? a.time.localeCompare(b.time)
: b.time.localeCompare(a.time);
}
});

Expand Down Expand Up @@ -245,10 +250,13 @@ function setDefaultDateTime() {

function sortTodos(sortBy) {
if (sortBy === 'priority') {
currentSortOrder = currentSortOrder === 'asc' ? 'desc' : 'asc';
currentCategorySortOrder = currentSortOrder === 'asc' ? 'desc' : 'asc';
} else if (sortBy === 'category') {
currentCategorySortOrder =
currentCategorySortOrder === 'asc' ? 'desc' : 'asc';
} else if (sortBy === 'time') {
currentCategorySortOrder =
currentCategorySortOrder === 'asc' ? 'desc' : 'asc';
}
currentSortMethod = sortBy;
updateTodoList();
Expand Down Expand Up @@ -305,6 +313,9 @@ document.addEventListener('DOMContentLoaded', () => {
document
.querySelector('.sort-button-priority')
.addEventListener('click', () => sortTodos('priority'));
document
.querySelector('.sort-button-time')
.addEventListener('click', () => sortTodos('time'));

// Add event listener for filter button
document
Expand Down
Loading