Skip to content

Commit 47f963e

Browse files
committed
Added alphabetical_sort() function
1 parent fd85d80 commit 47f963e

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

cmd2/cmd2.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def complete_users():
10921092
self.allow_closing_quote = False
10931093

10941094
# Sort the matches before any trailing slashes are added
1095-
matches.sort(key=str.lower)
1095+
matches = utils.alphabetical_sort(matches)
10961096
self.matches_sorted = True
10971097

10981098
# Build display_matches and add a slash to directories
@@ -1532,8 +1532,8 @@ def complete(self, text: str, state: int) -> Optional[str]:
15321532

15331533
# Sort matches alphabetically if they haven't already been sorted
15341534
if not self.matches_sorted:
1535-
self.completion_matches.sort(key=str.lower)
1536-
self.display_matches.sort(key=str.lower)
1535+
self.completion_matches = utils.alphabetical_sort(self.completion_matches)
1536+
self.display_matches = utils.alphabetical_sort(self.display_matches)
15371537
self.matches_sorted = True
15381538

15391539
try:
@@ -2325,12 +2325,10 @@ def _help_menu(self, verbose: bool=False) -> None:
23252325
"""Show a list of commands which help can be displayed for.
23262326
"""
23272327
# Get a sorted list of help topics
2328-
help_topics = self.get_help_topics()
2329-
help_topics.sort(key=str.lower)
2328+
help_topics = utils.alphabetical_sort(self.get_help_topics())
23302329

23312330
# Get a sorted list of visible command names
2332-
visible_commands = self.get_visible_commands()
2333-
visible_commands.sort(key=str.lower)
2331+
visible_commands = utils.alphabetical_sort(self.get_visible_commands())
23342332

23352333
cmds_doc = []
23362334
cmds_undoc = []

cmd2/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,12 @@ def remove_duplicates(list_to_prune: List) -> List:
157157
temp_dict[item] = None
158158

159159
return list(temp_dict.keys())
160+
161+
162+
def alphabetical_sort(list_to_sort: List[str]) -> List[str]:
163+
"""
164+
Sorts a list of strings alphabetically
165+
:param list_to_sort: the list being sorted
166+
:return: the sorted list
167+
"""
168+
return sorted(list_to_sort, key=str.casefold)

tests/test_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def test_delimiter_completion(cmd2_app):
410410

411411
# Remove duplicates from display_matches and sort it. This is typically done in complete().
412412
display_list = utils.remove_duplicates(cmd2_app.display_matches)
413-
display_list.sort(key=str.lower)
413+
display_list = utils.alphabetical_sort(display_list)
414414

415415
assert display_list == ['other user', 'user']
416416

0 commit comments

Comments
 (0)