@@ -154,6 +154,7 @@ def __init__(self, stdscr, initial_mode="list", initial_body=""):
154154 self .current_note_id : Optional [int ] = None
155155 self .search_term = ""
156156 self .selected_index = 0
157+ self .sort_desc = True
157158 self .last_selected_index = 0 # track last selection
158159 self .mode = initial_mode # "list", "edit", or "new"
159160 self .editing_body = initial_body # Pre-fill body if provided
@@ -199,7 +200,7 @@ def __init__(self, stdscr, initial_mode="list", initial_body=""):
199200
200201 def get_sorted_notes (self ):
201202 notes = self .note_manager .search_notes (self .search_term )
202- return sorted (notes , key = lambda x : x .id , reverse = True )
203+ return sorted (notes , key = lambda x : x .updated_at , reverse = self . sort_desc )
203204
204205 def draw_box (self , y : int , x : int , height : int , width : int , title : str = "" , color_pair : int = 0 ) -> None :
205206 # Safety check - don't draw if coordinates are out of bounds
@@ -497,8 +498,7 @@ def check_save_key(self, key: int) -> bool:
497498 def handle_list_mode (self , key : int ) -> bool :
498499 if self .show_search :
499500 if self .show_search :
500- notes = self .note_manager .search_notes (self .search_term )
501-
501+ notes = self .get_sorted_notes ()
502502 if key in (curses .KEY_UP , curses .KEY_DOWN ):
503503 if notes :
504504 if key == curses .KEY_UP :
@@ -511,7 +511,7 @@ def handle_list_mode(self, key: int) -> bool:
511511
512512 if key in (curses .KEY_BACKSPACE , 127 ):
513513 self .search_term = self .search_term [:- 1 ]
514- notes = self .note_manager . search_notes ( self . search_term )
514+ notes = self .get_sorted_notes ( )
515515 self .clamp_selection (notes )
516516 self .list_body_scroll = 0
517517 self .draw_list_view ()
@@ -521,7 +521,7 @@ def handle_list_mode(self, key: int) -> bool:
521521 self .search_term = ""
522522 self .draw_list_view ()
523523 elif key == 10 : # Enter
524- notes = self .note_manager . search_notes ( self . search_term )
524+ notes = self .get_sorted_notes ( )
525525 sorted_notes = self .get_sorted_notes ()
526526 if 0 <= self .selected_index < len (sorted_notes ):
527527 note = sorted_notes [self .selected_index ]
@@ -540,7 +540,7 @@ def handle_list_mode(self, key: int) -> bool:
540540 self .draw_edit_view ()
541541 elif 32 <= key <= 126 :
542542 self .search_term += chr (key )
543- notes = self .note_manager . search_notes ( self . search_term )
543+ notes = self .get_sorted_notes ( )
544544 self .selected_index = 0
545545 self .list_body_scroll = 0
546546 self .draw_list_view ()
@@ -563,9 +563,13 @@ def handle_list_mode(self, key: int) -> bool:
563563 self .selected_index = 0
564564 self .list_body_scroll = 0
565565 self .draw_list_view ()
566-
566+ elif key == ord ('s' ):
567+ self .sort_desc = not self .sort_desc
568+ self .selected_index = 0
569+ self .list_body_scroll = 0
570+ self .draw_list_view ()
567571 elif key == 10 : # Enter
568- notes = self .note_manager . search_notes ( self . search_term )
572+ notes = self .get_sorted_notes ( )
569573 sorted_notes = self .get_sorted_notes ()
570574 if 0 <= self .selected_index < len (sorted_notes ):
571575 note = sorted_notes [self .selected_index ]
@@ -584,7 +588,7 @@ def handle_list_mode(self, key: int) -> bool:
584588 self .draw_edit_view ()
585589
586590 elif key == ord ('d' ):
587- notes = self .note_manager . search_notes ( self . search_term )
591+ notes = self .get_sorted_notes ( )
588592 sorted_notes = self .get_sorted_notes ()
589593 if 0 <= self .selected_index < len (sorted_notes ):
590594 note = sorted_notes [self .selected_index ]
@@ -595,22 +599,22 @@ def handle_list_mode(self, key: int) -> bool:
595599 self .list_body_scroll = 0
596600
597601 elif key == curses .KEY_UP :
598- notes = self .note_manager . search_notes ( self . search_term )
602+ notes = self .get_sorted_notes ( )
599603 if notes :
600604 self .selected_index = (self .selected_index - 1 ) % len (notes )
601605 self .list_body_scroll = 0 # Reset scroll when changing notes
602606 self .draw_list_view ()
603607
604608 elif key == curses .KEY_DOWN :
605- notes = self .note_manager . search_notes ( self . search_term )
609+ notes = self .get_sorted_notes ( )
606610 if notes :
607611 self .selected_index = (self .selected_index + 1 ) % len (notes )
608612 self .list_body_scroll = 0 # Reset scroll when changing notes
609613 self .draw_list_view ()
610614
611615 # Handle preview scrolling with j/k keys
612616 elif key == ord ('j' ) or key == ord ('J' ):
613- notes = self .note_manager . search_notes ( self . search_term )
617+ notes = self .get_sorted_notes ( )
614618 if notes and 0 <= self .selected_index < len (notes ):
615619 # Calculate max scroll for current note
616620 note = notes [self .selected_index ]
@@ -632,7 +636,7 @@ def handle_list_mode(self, key: int) -> bool:
632636 self .draw_list_view ()
633637
634638 elif key == ord ('k' ) or key == ord ('K' ):
635- notes = self .note_manager . search_notes ( self . search_term )
639+ notes = self .get_sorted_notes ( )
636640 if notes and 0 <= self .selected_index < len (notes ):
637641 self .list_body_scroll = max (0 , self .list_body_scroll - 1 )
638642 self .draw_list_view ()
@@ -691,8 +695,8 @@ def handle_edit_mode(self, key: int):
691695 new_note = self .note_manager .add_note (self .editing_body )
692696
693697 # Find the index of the new note
694- notes = self .note_manager . search_notes ( self . search_term )
695- sorted_notes = sorted ( notes , key = lambda x : x . id )
698+ notes = self .get_sorted_notes ( )
699+ sorted_notes = self . get_sorted_notes ( )
696700 for i , note in enumerate (sorted_notes ):
697701 if note .id == new_note .id :
698702 self .selected_index = i
0 commit comments