diff --git a/IPython/html/services/notebooks/filenbmanager.py b/IPython/html/services/notebooks/filenbmanager.py index c3b2c05..820e80a 100644 --- a/IPython/html/services/notebooks/filenbmanager.py +++ b/IPython/html/services/notebooks/filenbmanager.py @@ -18,6 +18,7 @@ Authors: #----------------------------------------------------------------------------- import io +import locale import os import glob import shutil @@ -31,6 +32,10 @@ from IPython.utils.py3compat import getcwd from IPython.utils import tz from IPython.html.utils import is_hidden, to_os_path +def sort_key(item): + """Case-insensitive, locale aware sorting.""" + return locale.strxfrm(item['name'].lower()) + #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- @@ -189,7 +194,7 @@ class FileNotebookManager(NotebookManager): except IOError: pass dirs.append(model) - dirs = sorted(dirs, key=lambda item: item['name']) + dirs = sorted(dirs, key=sort_key) return dirs # TODO: Remove this after we create the contents web service and directories are @@ -230,7 +235,7 @@ class FileNotebookManager(NotebookManager): path = path.strip('/') notebook_names = self.get_notebook_names(path) notebooks = [self.get_notebook(name, path, content=False) for name in notebook_names] - notebooks = sorted(notebooks, key=lambda item: item['name']) + notebooks = sorted(notebooks, key=sort_key) return notebooks def get_notebook(self, name, path='', content=True): diff --git a/IPython/html/services/notebooks/tests/test_notebooks_api.py b/IPython/html/services/notebooks/tests/test_notebooks_api.py index ce90d53..e348f1d 100644 --- a/IPython/html/services/notebooks/tests/test_notebooks_api.py +++ b/IPython/html/services/notebooks/tests/test_notebooks_api.py @@ -101,7 +101,10 @@ class APITest(NotebookTestBase): ('foo', 'name with spaces'), ('foo', u'unicodé'), ('foo/bar', 'baz'), - (u'å b', u'ç d') + ('ordering', 'A'), + ('ordering', 'b'), + ('ordering', 'C'), + (u'å b', u'ç d'), ] hidden_dirs = ['.hidden', '__pycache__'] @@ -160,6 +163,11 @@ class APITest(NotebookTestBase): expected = [ u'a.ipynb', u'b.ipynb', u'name with spaces.ipynb', u'unicodé.ipynb'] expected = { normalize('NFC', name) for name in expected } self.assertEqual(nbnames, expected) + + nbs = notebooks_only(self.nb_api.list('ordering').json()) + nbnames = [n['name'] for n in nbs] + expected = ['A.ipynb', 'b.ipynb', 'C.ipynb'] + self.assertEqual(nbnames, expected) def test_list_dirs(self): dirs = dirs_only(self.nb_api.list().json())