1

Please help me to understand how to sort a nested array which looks like the one presented in the code below.

var arr2 = [
            [["a",1,10,4],["a",3,6,7]],[["a",44,34,10]],
            [["b",11,31,12],["b",110,88,77],["b",55,66,30]],
            [["c",12,36,18],["c",18,66,71],["c",52,45,89]]
           ];

In my case each letter represents a specific product type dataset I'd like to analyse separately. Hence it is important to me to sort each "product type array" on its own. Also important to know, the number of "product type arrays" is not fixed to three. It can be more or less. So the code should be flexible enough to handle a variable number of "product type arrays".

In my example, let's say the first numeric value after product type identifier is the value I'd like to use for sorting. How can I do that?

I know how the sort function works in a less complex scenario but can't figure out how to apply this to the data above.

var arr2 = [
            ["a",1,10,4],["a",3,6,7],["a",44,34,10]
           ];

var newArr2 = arr2.sort(function(r1,r2) {
  var a = r1[1];
  var b = r2[1];

  return a-b;
});

Thanks for your help!


Edit 20/05/20:

A more realistic data example that actually matches my data.

  var arr4 = [[["hansa bank - blau", "hansa bank - blau blau", 1, 10, 9], ["hansa bank - blau", "hansa bank - blau blau", 3, 6, 7], ["hansa bank - blau", "hansa bank - blau blau", 44, 34, 10]], 
              [["hansa bank - grün", "hansa bank - grün grün", 11, 31, 105], ["hansa bank - grün", "hansa bank - grün grün", 110, 88, 77], ["hansa bank - grün", "hansa bank - grün grün", 55, 66, 30]], 
              [["hansa bank - rot", "hansa bank - rot rot", 12, 36, 33], ["hansa bank - rot", "hansa bank - rot rot", 18, 66, 12], ["hansa bank - rot", "hansa bank - rot rot", 52, 45, 89]]];

1 Answer 1

1

You could iterate and apply the sorting for each array.

var arr2 = [[["a", 1, 10, 4], ["a", 3, 6, 7]], [["a", 44, 34, 10]], [["b", 11, 31, 12], ["b", 110, 88, 77], ["b", 55, 66, 30]], [["c", 12, 36, 18], ["c", 18, 66, 71], ["c", 52, 45, 89]]];

arr2.forEach(a => a.sort(({ 1: a }, { 1: b }) => a - b));

console.log(arr2);

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the code Nina. It worked flawlessly. One question though. Could you please tell me how to change the sort order from descending to ascending? It's more complex than just changing the code to b - a. I already tried that. It seems I need to get more comfortable with your code though. Anyway, I'll accept your answer since it fits my needs.
ascending is usually a - b and descending the other one b - a. but what do you want?
I added another, more realistic data example which matches my data more precisely. The problem I'm facing when applying your code – and that's why I said I already tried b - a, is that your code only works for descending order. I tried the following code with my data: arr4.forEach(a => a.sort(({ 4: a }, { 4: b }) => a - b)); which works fine. However, when changing to b - a the first nested array does not get sorted. Only the second and third one.
i get an ordered array.
Interesting! I tried it with my local IDE. It works fine. Must have been a problem with the Google Apps Script Editor. I refreshed it and now it works just fine. Thanks again for your help and patience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.