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:
- the numbers from minor to mayor
- then the words, the capital letter doesn't matter.
- 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
arr.sort(sortFunction);. which takes 2 parameters. Inside the sort function, you'll need to first compare if both parameters are both numberlike, if they are, return the appropriate integer for their comparison (with number-like strings compared as numbers) and if they are not, then set both to uppercase and compare them lexicographically.