Added sorting functionality

This commit is contained in:
Michael Rowley 2021-12-29 23:31:39 +00:00
parent 8ae5fd084c
commit 588cd93900
2 changed files with 20 additions and 4 deletions

View file

@ -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;
}

View file

@ -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.
*