I've been working on a sorting algorithm to intelligently sort filesizes regardless of the input format. The function is designed to sort an array of string inputs to produce the following desired outputs;
input = ["5.1 GB","19934563 B","224 kB","0.55 GB","0.04 kB","0.02 TB","2.4 MB"]
output = ["0.04 kB","224 kB","2.4 MB","19934563 B","0.55 GB","5.1 GB","0.02 TB"]
The code preprocesses the input to a specific format for easier manipulation before sorting the values. It currently produces the following result
input = ["5.1 GB ","19934563 B"," 224 kB"," 0.55 GB ","0.04 kB ","0.02 TB","2.4\tMB",]
output =["0.04 kB ","224 kB","2.4 MB","19934563 B","0.55 GB","5.1 GB","0.02 TB",]
My Inquiry: Is there a way to revert back to the original string format after sorting, or how can I achieve the required outcome? That is, having the filesize string sorted intelligently in the original input format.
Below is the snippet of my code:
function sort(fileSizes, descending) {
if (!fileSizes.length) return "Enter an List to be Sorted";
else {
let validFormat = fileSizes.map((file) => validFile(file));
if (validFormat.includes(false)) {
let formarted = fileSizes.map(item => item.replace(/\s+/g,' ').trim());
fileSizes = formarted;
};
let sortedFiles = sortSizes(fileSizes);
if (descending === true ) return sortedFiles;
if (descending === false) return sortedFiles.reverse();
}
}
//////////////////////////////////////////////
//Helper Functions
//Regular expression for valid input format
function validFile(str) {
let regEx = /^(-?\d*(\.\d+)?)\s((T|G|M|k)*B)$/;
let valid = regEx.test(str);
return valid;
}
// global comparator array
let sizes = ["B", "kB", "MB", "GB", "TB"];
//Custom Sorter
function sortSizes(arr) {
// sort by comparator
arr.sort(function(x, y) {
var x_res = x.split(" "), y_res = y.split(" ");
var x_value = x_res[0], x_unit = x_res[1];
var y_value = y_res[0], y_unit = y_res[1];
let amount = casting(x_unit, y_unit, x_value);
if(amount < y_value) {
return -1;
} else if(x_value > y_value) {
return 1;
} else {
return 0;
}
});
return arr.reverse();
}
//Convert file unit for comparison
function casting(unit_from, unit_to, amount) {
var i = sizes.indexOf(unit_from);
var j = sizes.indexOf(unit_to);
var r;
if(i < j) {
r = j - i;
} else {
r = j - i;
}
var i = 0;
if(r < 0) {
r *= (-1);
while(i < r) {
amount *= 1024;
i++;
}
} else {
while(i < r) {
amount /= 1024;
i++;
}
}
return amount;
}
console.log(sort(["5.1 GB","19934563 B","224 kB","0.55 GB","0.04 kB","0.02 TB","2.4 MB",], false)); // ["0.04 kB","224 kB","2.4 MB","19934563 B","0.55 GB","5.1 GB","0.02 TB",]
console.log(sort(["5.1 GB","19934563 B","224 kB","0.55 GB","0.04 kB","0.02 TB","2.4 MB",], true)); //["0.02 TB","5.1 GB","0.55 GB","19934563 B","2.4 MB","224 kB","0.04 kB"]
console.log(sort([], true)); //"Enter an List to be Sorted"
console.log(sort(["5.1 GB ","19934563 B"," 224 kB"," 0.55 GB ","0.04 kB ","0.02 TB","2.4\tMB",], false)); //["0.04 kB "," 224 kB","2.4\tMB","19934563 B"," 0.55 GB ","5.1 GB ","0.02 TB",]