From fcbcbbfb4ddf4f521b365df65914540faefd3e6c Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 6 Apr 2025 18:37:45 +0200 Subject: [PATCH] FR: Sort Sensors my name in proper number order #3132 --- glances/globals.py | 9 +++++++++ glances/plugins/sensors/__init__.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/glances/globals.py b/glances/globals.py index 7bd291ce..f53847b2 100644 --- a/glances/globals.py +++ b/glances/globals.py @@ -508,3 +508,12 @@ def replace_special_chars(input_string, by=' '): """Replace some special char by another in the input_string Return: the string with the chars replaced""" return input_string.replace('\r\n', by).replace('\n', by).replace('\t', by) + + +def atoi(text): + return int(text) if text.isdigit() else text + + +def natural_keys(text): + """Return a text in a natural/human readable format.""" + return [atoi(c) for c in re.split(r'(\d+)', text)] diff --git a/glances/plugins/sensors/__init__.py b/glances/plugins/sensors/__init__.py index 41940009..80233d1c 100644 --- a/glances/plugins/sensors/__init__.py +++ b/glances/plugins/sensors/__init__.py @@ -14,7 +14,7 @@ from typing import Any import psutil -from glances.globals import to_fahrenheit +from glances.globals import natural_keys, to_fahrenheit from glances.logger import logger from glances.outputs.glances_unicode import unicode_message from glances.plugins.plugin.model import GlancesPluginModel @@ -144,7 +144,7 @@ class PluginModel(GlancesPluginModel): # Remove duplicates thanks to https://stackoverflow.com/a/9427216/1919431 stats_transformed = [dict(t) for t in {tuple(d.items()) for d in stats_transformed}] # Sort by label - return sorted(stats_transformed, key=lambda d: d['label']) + return sorted(stats_transformed, key=lambda d: natural_keys(d['label'])) @GlancesPluginModel._check_decorator @GlancesPluginModel._log_result_decorator