Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to sort array in by numbers then letters then symbols?

I am trying to order this array in Javascript

arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; 

And i want that the array is showed like this:

["3", "4", "10", "23", "42", "63", "ABC", "adidas", "ax", "ba", "mof", ")", "["]

Considering the symbols ")" , "[" too.

The order that i want to be the array is:

  1. the numbers from minor to mayor
  2. then the words, the capital letter doesn't matter.
  3. Finally, the symbols.

I know that i must use sort(), but the problem is i can't order the array with the symbols, i have that problem

I am using that code to show the array and the ordered array in HTML

var arr, text, larr, i; 
arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"]; 
larr = arr.length;

text = "<ul>";
for (i = 0; i < larr; i++) {
  text += "<li>" + arr[i] + "</li>";
}
text += "</ul>";

document.getElementById("arrayin").innerHTML = text;

console.log(arr);

var arror,textor, larror, j;

arror = arr.sort();

larror = arror.length;

textor = "<ul>";
for (j = 0; j < larror; j++) {
  textor += "<li>" + arror[j] + "</li>";
}
textor += "</ul>";

console.log(arror);

document.getElementById("Oarray").innerHTML = textor;

My final question is how can i order the array with the numbers, letters and symbols

like image 346
J1293 Avatar asked Oct 11 '18 17:10

J1293


People also ask

How do you sort an array element alphabetically?

In JavaScript arrays have a sort( ) method that sorts the array items into an alphabetical order. The sort( ) method accepts an optional argument which is a function that compares two elements of the array. If the compare function is omitted, then the sort( ) method will sort the element based on the elements values.

How do you sort an array of objects alphabetically in JavaScript?

Recall that by default, to sort an array containing primitive values (such as strings) alphabetically, you would simply call the sort() method without any comparison function passed in directly on the array, such as myarray. sort() .

What is the correct method to use in JavaScript sorting arrays?

Even if objects have properties of different data types, the sort() method can be used to sort the array.

What is a correct method to sort the elements of an array?

The sort() method allows you to sort elements of an array in place. Besides returning the sorted array, the sort() method changes the positions of the elements in the original array. By default, the sort() method sorts the array elements in ascending order with the smallest value first and largest value last.


1 Answers

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);
like image 55
Tope Avatar answered Oct 02 '22 03:10

Tope