From 588cd93900633abf15ffc503f3f3ac640e50bcfd Mon Sep 17 00:00:00 2001 From: Michael Rowley Date: Wed, 29 Dec 2021 23:31:39 +0000 Subject: [PATCH] Added sorting functionality --- src/core/lib/IP.mjs | 1 + src/core/operations/Sort.mjs | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/core/lib/IP.mjs b/src/core/lib/IP.mjs index 08c712e0..c43a0797 100644 --- a/src/core/lib/IP.mjs +++ b/src/core/lib/IP.mjs @@ -351,6 +351,7 @@ export function ipv4ToStr(ipInt) { */ export function strToIpv6(ipStr, retArr=true) { if (retArr === false) { + // TODO: Expand address. if (ipStr.length % 2 === 1) { ipStr = "0" + ipStr; } diff --git a/src/core/operations/Sort.mjs b/src/core/operations/Sort.mjs index a1148f7c..cd3b8e76 100644 --- a/src/core/operations/Sort.mjs +++ b/src/core/operations/Sort.mjs @@ -38,7 +38,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)", "IPv4 address", "IPv6 address", "Numeric", "Numeric (hexadecimal)"] } ]; } @@ -58,8 +58,10 @@ class Sort extends Operation { sorted = sorted.sort(); } else if (order === "Alphabetical (case insensitive)") { sorted = sorted.sort(Sort._caseInsensitiveSort); - } else if (order === "IP address") { - sorted = sorted.sort(Sort._ipSort); + } else if (order === "IPv4 address") { + sorted = sorted.sort(Sort._ipv4Sort); + } else if (order == "IPv6 address") { + sorted = sorted.sort(Sort._ipv6Sort); } else if (order === "Numeric") { sorted = sorted.sort(Sort._numericSort); } else if (order === "Numeric (hexadecimal)") { @@ -91,7 +93,7 @@ class Sort extends Operation { * @param {string} b * @returns {number} */ - static _ipSort(a, b) { + static _ipv4Sort(a, b) { let a_ = a.split("."), b_ = b.split("."); @@ -105,6 +107,19 @@ class Sort extends Operation { return a_ - b_; } + /** + * Comparison operator for sorting of IPv6 addresses. + * + * @param {string} a + * @param {string} b + * @returns {number} + */ + static _ipv6Sort(a, b) { + const numericalA = strToIpv6(a, false), + numericalB = strToIpv6(b, false); + return numericalA < numericalB ? -1 : 1; + } + /** * Comparison operation for sorting of numeric values. *