Sort order of CompletionItems display #1154
-
If I have a list of CompletionItems like this: When I tab-complete the command, it displays the completion items like this:
How do I get them to display in numerical order of item ID's instead of alphabetical order? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
First sort the results how you want them to display. Then set Here is a description of # Set to True before returning matches to complete() in cases where matches have already been sorted.
# If False, then complete() will sort the matches using self.default_sort_key before they are displayed.
# This does not affect self.formatted_completions.
self.matches_sorted = False One thing to note is that You can do either of the following: from cmd2 import utils
# 1. This way sorts in place
my_list.sort(key=utils.natural_keys)
# 2. This way makes a copy of the list
my_list = utils.natural_sort(my_list) If you sort the ID numbers before creating your |
Beta Was this translation helpful? Give feedback.
First sort the results how you want them to display. Then set
self.matches_sorted = True
before returning them.Here is a description of
matches_sorted
from cmd2.py. This value gets reset toFalse
each time tab completion runs, so you don't need to reset it manually.One thing to note is that
CompletionItem
inherits from string. Therefore the IDs are being converted to strings when you create eachCompletion…