In my array, I have a mixture of strings and arrays. Each string corresponds to the array next to it. For example, the string 789 is associated with the array ['111A', '222B', '333C']. My goal is to sort based on the strings while maintaining the connection to the relevant array.
I attempted to use the sort()
method, which works fine when dealing only with strings in the array. However, as soon as I introduce arrays, the default sorting behavior returns.
let myArray = [
'789',
['111A','222B','333C'],
'456',
['444E','555F','666G'],
'123',
['777H','888I','999J']
]
myArray.sort(function(a,b){
return a - b
})
Ultimately, I want the final data to be sorted like this:
['123', ['777H', '888I','999J'],
'456', ['444E', '555F', '666G'],
'789', ['111A', '222B', '333C']]