Skip to main content

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1; if (wordB) return 1; return 1; // or whatever logic to sort within non-alphanumeric values } 

Now, use it in your sort method call.

function 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) return -1; if (wordB) return 1; return 1; // or whatever logic to sort within non-alphanumeric values } // Now, use it in your sort method call. let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; arr.sort(compare); console.log(arr);
let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; arr.sort(compare); console.log(arr); 
 

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1; if (wordB) return 1; return 1; // or whatever logic to sort within non-alphanumeric values } 

Now, use it in your sort method call.

let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; arr.sort(compare); console.log(arr); 

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1; if (wordB) return 1; return 1; // or whatever logic to sort within non-alphanumeric values } // Now, use it in your sort method call. let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; arr.sort(compare); console.log(arr);
 

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1;   if (wordB) return 1;   return 1; // or whatever logic to sort within non-alphanumeric values } 

Now, use it in your sort method call.

let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; } arr.sort(compare); console.log(arr); 

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1; if (wordB) return 1; return 1; //or whatever logic to sort within non-alphanumeric values } 

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1;   if (wordB) return 1;   return 1; // or whatever logic to sort within non-alphanumeric values } 

Now, use it in your sort method call.

let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"];  arr.sort(compare); console.log(arr); 
Source Link
Tope
  • 1k
  • 2
  • 12
  • 16

As you guessed you do have to use the sort method and pass in a compare function. In order to get the order you are looking for the following checks will work. Full working example on jsfiddle here - https://jsfiddle.net/aczhyk8j/1/

function 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) return -1; if (wordB) return 1; return 1; //or whatever logic to sort within non-alphanumeric values }