// Comparator function to sort strings as decimal numbers
function compareAsDecimal(a, b) {
// Convert strings to numbers for comparison
const numA = parseFloat(a);
const numB = parseFloat(b);
return numA - numB;
}
// Input array of strings
const numbers = ["11.3", "3.6", "89.1", "3.14", "456.0", "0", "0.001", "2.0"];
// Sort the array using the custom comparator
numbers.sort(compareAsDecimal);
console.log("Sorted array of decimal strings:");
console.log(numbers.join(" "));
/*
run:
Sorted array of decimal strings:
0 0.001 2.0 3.14 3.6 11.3 89.1 456.0
*/