From cb023089bbadd609ae9daefd426bdb86ea735887 Mon Sep 17 00:00:00 2001 From: Didier Stevens Date: Sun, 30 Oct 2022 15:33:11 +0100 Subject: [PATCH] Operation Sort: added value Length to option Order --- src/core/lib/Sort.mjs | 12 ++++++++++++ src/core/operations/Sort.mjs | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/lib/Sort.mjs b/src/core/lib/Sort.mjs index 46bbebd9..adb30d71 100644 --- a/src/core/lib/Sort.mjs +++ b/src/core/lib/Sort.mjs @@ -103,3 +103,15 @@ export function hexadecimalSort(a, b) { return a.localeCompare(b); } + +/** + * Comparison operation for sorting of lines by length + * + * @param {string} a + * @param {string} b + * @returns {number} + */ +export function lengthSort(a, b) { + return a.length - b.length; +} + diff --git a/src/core/operations/Sort.mjs b/src/core/operations/Sort.mjs index 19e4cbb2..7a4714be 100644 --- a/src/core/operations/Sort.mjs +++ b/src/core/operations/Sort.mjs @@ -7,7 +7,7 @@ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs"; -import {caseInsensitiveSort, ipSort, numericSort, hexadecimalSort} from "../lib/Sort.mjs"; +import {caseInsensitiveSort, ipSort, numericSort, hexadecimalSort, lengthSort} from "../lib/Sort.mjs"; /** * Sort operation @@ -39,7 +39,7 @@ class Sort extends Operation { { "name": "Order", "type": "option", - "value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric", "Numeric (hexadecimal)"] + "value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric", "Numeric (hexadecimal)", "Length"] } ]; } @@ -65,6 +65,8 @@ class Sort extends Operation { sorted = sorted.sort(numericSort); } else if (order === "Numeric (hexadecimal)") { sorted = sorted.sort(hexadecimalSort); + } else if (order === "Length") { + sorted = sorted.sort(lengthSort); } if (sortReverse) sorted.reverse();