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:
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
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.
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() .
Even if objects have properties of different data types, the sort() method can be used to sort the 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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With