I am looking to arrange a two-dimensional array containing both numbers and letters using JScript, not Javascript. Even though JScript is the native Microsoft JS engine used in Windows Scripting and Internet Explorer, I will still refer to it as JavaScript for simplicity's sake. Here is the array I want to sort:
var arr = [
['D1-45-32', 'Sneaker'],
['11-12-54', 'Ball'],
['34-56-12', 'House'],
['Y5-78-89', 'Coke'],
['11-11-11', 'Ball_1']
];
The desired order after sorting should be:
[
['11-11-11', 'Ball_1'],
['11-12-54', 'Ball'],
['34-56-12', 'House'],
['D1-45-32', 'Sneaker'],
['Y5-78-89', 'Coke']
]
My attempted solution involved using the following code block:
arr.sort(function(a,b){
return /[A-Za-z]/.test(a) - /[A-Za-z]/.test(b) || a.charCodeAt(0) - b.charCodeAt(0)
});
console.log(arr);
Is there a simpler way to achieve this sorting using a loop? Any suggestions would be appreciated. Thank you!