What is the best approach to solve the following problem in Javascript? I have attempted to solve it but i am running into too much trouble, it is not even worth it to post my current code, it's an ugly mess.
I have a multidimensional array 'Teams' with multiple arrays that have a TeamName (string), WinRecord (number), LossRecord (number), and a Sort_Key (number) in this format: [TeamName, W, L, sort_key]
My data looks like this:
Teams = [
['Team A', 25, 2, 88],
['Team B', 20, 7, 84],
['Team C', 20, 7, 76],
['Team D', 20, 7, 54],
['Team E', 20, 7, 65],
['Team F', 20, 6, 34],
['Team G', 19, 8, 67],
['Team H', 20, 7, 11],
...
['Team N', 3, 24, 33]
]
The previous data is in an arbitrary order for a reason.
What I want to do is to look inside the Multidimensional Array "Teams" where the W-L record is the same in the adjacent arrays. From the previous data, teams B, C, D and E have the same W-L record and they are all adjacent to each other. Team F does NOT have the same because although it matches the W but it does not match the L. Notice that team H has the same 20-7 record but since it is NOT adjacent, we do not consider it for our comparison!
Now I want to re-organise the adjacent teams B, C, D and E , in ASC order by sort_key. The arrays that do not have a matching W-L with another array must stay in the same position. Therefore the solution would look like this:
Teams = [
['Team A', 25, 2, 88], //stays in the same place because it has no W-L match adjacent
['Team D', 20, 7, 54], //sort_key is the lowest of this "chunk" so this goes first
['Team E', 20, 7, 65], //sort_key second in ASC order
['Team C', 20, 7, 76], //then this one
['Team B', 20, 7, 84], //finally this is the highest sort_key of this "chunk"
['Team F', 20, 6, 34], //stays in the same place because it has no W-L match adjacent
['Team G', 19, 8, 67], //stays in the same place because it has no W-L match adjacent
['Team H', 20, 7, 11], //stays in the same place because its NOT ADJACENT to the last 20-7 team
...
['Team N', 3, 24, 33] //stays in the same place because it has no W-L match adjacent
]
Note:The previous example has adjacent arrays with the same W-L of 20-7 , but there might be more "chunks" with matching W-L arrays that are adjacent to each other, this may happen multiple times until we reach array at position 'n'.
What I have in mind is:
To take the first array and compare with the next array, if there is a W-L match to check the next one, n times until there is no further match. If there is no match we move on to the next array and perform this step again.
If there is an adjacent W-L we need to keep checking the next array until there is no exact match and we stop. All the adjacent matching arrays can be copied to a temporary array. We need to save "from" the original_position because if we take data from Teams[2] all the way to Teams[5] we need to re-insert the sorted values later starting at position 2. Maybe save this original_position into a variable?
We now sort the temporary array by sort_key ASC. We now copy the temp array into the original Teams array "from" the original_position.
We keep going looking for more matches like this until we reach position 'n' and we can exit and return the finalised properly sorted Teams array.
It doesn't seem so complicated but I cannot figure out how to compare multidimensional arrays with two matching values (W-L)... Please help :)