sort
This splits the input with a comma and then sorts the array numerically or by string. The order parameter controls the direction.
Created by: hackvertor
Installed 1 times
Category: Array
Created on: Tuesday, October 15, 2024 at 7:09:16 PM
Updated on: Tuesday, October 15, 2024 at 7:09:16 PM
Tag arguments
[
{
"type": "string",
"help": "This provides the sort order",
"defaultValue": "asc"
}
]
Code
class sort {
encode(input, order) {
const array = input.split(",");
const isNumeric = array.every((item) => /^[\d.-]+$/.test(item));
if (isNumeric) {
return array.sort((a, b) => (order === "asc" ? +a - +b : +b - +a));
} else {
return array.sort((a, b) =>
order === "asc"
? a.toString().localeCompare(b.toString())
: b.toString().localeCompare(a.toString()),
);
}
}
}