JSFiddle - React, Tailwind, and code Playground
JavaScript
let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"];
//desired order - ["3", "4", "10", "23", "42", "63", "ABC", "adidas", "ax", "ba", "mof", ")", "["]
console.log(arr);
const compare = (a,b) => {
// check for numberhood
const numA = !isNaN(a);
const numB = !isNaN(b);
if(numA && numB){
return Number(a) - Number(b);
}
if(numA) return -1;
if(numB) return 1;
// check for wordhood
const wordA = /^[a-zA-Z]+$/.test(a);
const wordB = /^[a-zA-Z]+$/.test(b);
if(wordA && wordB){
return a.localeCompare(b);
}
if(wordA || wordB) return (wordA && -1) || 1;
return 1; //or whatever logic to sort within non-alphanumeric values
}
arr = arr.sort(compare);
console.log(arr);