Skip to content

Commit 1998c97

Browse files
committed
Improve mask name sorting
Break the name into chunks so that we sort strings correctly and sort integers numerically ([1, 2, ..., 10, ..., 20, ...]) rather than as strings ([1, 10, ..., 2, 20, ...]). Signed-off-by: Brianna Major <brianna.major@kitware.com>
1 parent b02ba65 commit 1998c97

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

hexrdgui/masking/mask_manager_dialog.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import h5py
55
from itertools import groupby
66
from operator import attrgetter
7+
import re
78

89
from PySide6.QtCore import QObject, Qt
910
from PySide6.QtWidgets import (
@@ -44,6 +45,7 @@ def __init__(self, parent=None):
4445
self.setup_connections()
4546

4647
def show(self):
48+
self.create_tree()
4749
self.ui.show()
4850

4951
def setup_connections(self):
@@ -146,6 +148,15 @@ def remove_mask_item(self, name):
146148
self.mask_tree_items.pop(parent.text(0))
147149
MaskManager().masks_changed()
148150

151+
152+
def _alphanumeric_sort(self, value):
153+
# Split the string into text and number parts so that we
154+
# sort by string value lexicographically and the number
155+
# value numerically
156+
vals = re.split('([0-9]+)', value)
157+
print([int(v) if v.isdigit() else v.lower() for v in vals])
158+
return [int(v) if v.isdigit() else v.lower() for v in vals]
159+
149160
def update_tree(self):
150161
if self.ui.masks_tree.topLevelItemCount() == 0:
151162
self.create_tree()
@@ -190,9 +201,11 @@ def create_tree(self):
190201
# Create mode item
191202
mode_item = self.create_mode_item(mode, source)
192203

193-
# Create items for each mask
194-
for mask in masks:
204+
# Create items for each mask, sorted naturally by name
205+
for mask in sorted(masks,
206+
key=lambda x: self._alphanumeric_sort(x.name)):
195207
self.create_mask_item(mode_item, mask)
208+
196209
self.ui.masks_tree.expandAll()
197210
self.ui.masks_tree.resizeColumnToContents(0)
198211
self.ui.masks_tree.resizeColumnToContents(1)

0 commit comments

Comments
 (0)