Selaa lähdekoodia

wxGUI: natural sort for map selection and add many maps dialog, https://trac.osgeo.org/grass/ticket/2818

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@67070 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 9 vuotta sitten
vanhempi
commit
4d986c6d93

+ 2 - 1
gui/wxpython/gui_core/dialogs.py

@@ -33,6 +33,7 @@ import re
 import wx
 
 from grass.script import core as grass
+from grass.script.utils import natural_sort
 
 from grass.pydispatch.signal import Signal
 
@@ -1403,7 +1404,7 @@ class MapLayersDialogBase(wx.Dialog):
         :param str mapset: mapset name
         """
         self.map_layers = grass.list_grouped(type = type)[mapset]
-        self.layers.Set(self.map_layers)
+        self.layers.Set(natural_sort(self.map_layers))
         
         # check all items by default
         for item in range(self.layers.GetCount()):

+ 1 - 1
gui/wxpython/gui_core/gselect.py

@@ -556,7 +556,7 @@ class TreeCtrlComboPopup(ListCtrlComboPopup):
         :param exclude: True to exclude, False for forcing the list
         :param node: parent node
         """
-        elist.sort()
+        elist = grass.natural_sort(elist)
         for elem in elist:
             if elem != '':
                 fullqElem = elem + '@' + mapset

+ 12 - 0
lib/python/script/utils.py

@@ -22,6 +22,7 @@ import sys
 import shutil
 import locale
 import shlex
+import re
 
 def float_or_dms(s):
     """Convert DMS to float.
@@ -251,3 +252,14 @@ def split(s):
         return shlex.split(s.replace('\\', r'\\'))
     else:
         return shlex.split(s)
+
+
+# source:
+#    http://stackoverflow.com/questions/4836710/
+#    does-python-have-a-built-in-function-for-string-natural-sort/4836734#4836734
+def natural_sort(l):
+    """Returns sorted strings using natural sort
+    """
+    convert = lambda text: int(text) if text.isdigit() else text.lower()
+    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
+    return sorted(l, key=alphanum_key)