From 0b9ecd7d20be1bfc131688d2c09691e7e8618cfb Mon Sep 17 00:00:00 2001 From: Fabio Missagia <75183735+F4bbi@users.noreply.github.com> Date: Fri, 3 May 2024 20:02:13 +0200 Subject: [PATCH] Fix sorting order for numeric strings (#186) --- src/helpers/filetreeUtils.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/helpers/filetreeUtils.js b/src/helpers/filetreeUtils.js index a771aa7..2e3e1f2 100644 --- a/src/helpers/filetreeUtils.js +++ b/src/helpers/filetreeUtils.js @@ -24,6 +24,17 @@ const sortTree = (unsorted) => { return -1; } + //Regular expression that extracts any initial decimal number + const aNum = parseFloat(a.match(/^\d+(\.\d+)?/)); + const bNum = parseFloat(b.match(/^\d+(\.\d+)?/)); + + const a_is_num = !isNaN(aNum); + const b_is_num = !isNaN(bNum); + + if (a_is_num && b_is_num && aNum != bNum) { + return aNum - bNum; //Fast comparison between numbers + } + if (a.toLowerCase() > b.toLowerCase()) { return 1; }