I need to convert an array with two-digit number strings and sort them at the end
const array = ['0', '1', '10', '11', '12-13', '2', '3', '4-5-6-7-8', '9'];
const desiredResult = ['0', '1', '2', '3', '4-5-6-7-8', '9', '10', '11', '12-13']
I used below the chatGPT solution but it gives wrong result. please help
array.sort((a, b) => {
// Parse strings to numbers if they are not two-digit numbers
a = parseInt(a, 10);
b = parseInt(b, 10);
// If both are two-digit numbers, maintain their relative order
if (a >= 10 && b >= 10) {
return 0;
}
// If 'a' is a two-digit number and 'b' is not, move 'a' to the end
if (a >= 10 && b < 10) {
return 1;
}
// If 'b' is a two-digit number and 'a' is not, move 'b' to the end
if (a < 10 && b >= 10) {
return -1;
}
// Otherwise, sort 'a' and 'b' as normal
return a - b;
});
console.log(array);
// it is still the same and not sorted