summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/defaults/keys.py6
-rw-r--r--ranger/fsobject/directory.py18
2 files changed, 21 insertions, 3 deletions
diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py
index 9f0c78cb..21beebeb 100644
--- a/ranger/defaults/keys.py
+++ b/ranger/defaults/keys.py
@@ -202,12 +202,12 @@ map('zm', fm.toggle_boolean_option('mouse_enabled'))
 map('zf', fm.open_console('filter '))
 
 # ------------------------------------------------------------ sort
-map('o<bg>', 'O<bg>', fm.hint("*s*ize *b*ase*n*ame *m*time" \
-	" *t*ype *r*everse"))
+map('o<bg>', 'O<bg>', fm.hint("*s*ize *b*asename *m*time" \
+	" *t*ype *r*everse *n*umeric"))
 sort_dict = {
 	's': 'size',
 	'b': 'basename',
-	'n': 'basename',
+	'n': 'numeric',
 	'm': 'mtime',
 	't': 'type',
 }
diff --git a/ranger/fsobject/directory.py b/ranger/fsobject/directory.py
index 9bdb4caa..ab12734f 100644
--- a/ranger/fsobject/directory.py
+++ b/ranger/fsobject/directory.py
@@ -15,6 +15,7 @@
 
 import os.path
 import stat
+import re
 from stat import S_ISLNK, S_ISDIR
 from os import stat as os_stat, lstat as os_lstat
 from os.path import join, isdir, basename
@@ -39,6 +40,18 @@ def sort_by_directory(path):
 	"""returns 0 if path is a directory, otherwise 1 (for sorting)"""
 	return 1 - path.is_directory
 
+def sort_by_number(path):
+	try:
+		return float(re.search(r'\d+', path.basename).group(0))
+	except:
+		return path.basename
+
+def sort_by_number_icase(path):
+	try:
+		return float(re.search(r'\d+', path.basename).group(0))
+	except:
+		return path.basename_lower
+
 def accept_file(fname, hidden_filter, name_filter):
 	if hidden_filter:
 		try:
@@ -76,6 +89,7 @@ class Directory(FileSystemObject, Accumulator, SettingsAware):
 
 	sort_dict = {
 		'basename': sort_by_basename,
+		'numeric': sort_by_number,
 		'size': lambda path: -path.size,
 		'mtime': lambda path: -(path.stat and path.stat.st_mtime or 1),
 		'type': lambda path: path.mimetype,
@@ -295,6 +309,10 @@ class Directory(FileSystemObject, Accumulator, SettingsAware):
 				sort_func == sort_by_basename:
 			sort_func = sort_by_basename_icase
 
+		if self.settings.sort_case_insensitive and \
+				sort_func == sort_by_number:
+			sort_func = sort_by_number_icase
+
 		self.files.sort(key = sort_func)
 
 		if self.settings.sort_reverse: