2

I want to sort a multidimensional array using the first row, I want that the second and the third row get the same sort order of the first row

Here is an example of array :

var arr = 
[
[1,3,5,6,0,2],
[1,2,7,4,0,6],
[1,2,3,4,5,6]
]

the result that I want =

[
[0,1,2,3,5,6],
[0,1,6,2,7,4],
[5,1,6,2,3,4]
]

I tried the following function but it doesnt work as I want

arr = arr.sort(function(a,b) {
  return a[0] > b[0];
});

What's wrong with my function ? Thank you

11
  • 4
    Could you post the desired result? Commented Feb 17, 2016 at 23:02
  • not clear what description of column sort means Commented Feb 17, 2016 at 23:02
  • 1
    Another thing is that .sort should return a number, not a bool. Commented Feb 17, 2016 at 23:04
  • So why don't you create an array with [arr[0],arr[0],arr[0]] will have the same result. Commented Feb 17, 2016 at 23:08
  • 2
    I believe OP is saying that if item in index N of the first array is moved to index M then whatever element is in arrays 2 and 3 at index N should be moved to index M. Commented Feb 17, 2016 at 23:10

2 Answers 2

4

I think I understand what you're looking for:

a = [
[1,3,5,6,0,2],
[1,2,7,4,0,6],
[1,2,3,4,5,6]
]

transpose = m => m[0].map((_, c) => m.map(r => r[c]));

z = transpose(transpose(a).sort((x, y) => x[0] - y[0]))

z.map(r => 
    document.write('<pre>'+JSON.stringify(r) + '</pre>'));

In ES5

transpose = function(m) {
    return m[0].map(function (_, c) {
        return m.map(function (r) {
            return r[c]
        })
    })
};

z = transpose(transpose(a).sort(function (x, y) { return x[0] - y[0] }));

UPD: the transpose trick is kinda "smart", but hardly efficient, if your matrices are big, here's a faster way:

a = [
[1,3,5,6,0,2],
[1,2,7,4,0,6],
[1,2,3,4,5,6]
]

t = a[0].map((e, i) => [e, i]).sort((x, y) => x[0] - y[0]);
z = a.map(r => t.map(x => r[x[1]]));

z.map(r => 
    document.write('<pre>'+JSON.stringify(r) + '</pre>'));

Basically, sort the first row and remember indexes, then for each row, pick values by an index.

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

9 Comments

Could you translate that into English, please?
@MikeC transposing just flips the matrix (rows become columns columns become rows)
probably lack of arrow function support
@taboubim: your platform probably doesn't support fat arrow functions. Replace them with normal functions.
@taboubim: added an ES5 version.
|
1

A bit more verbose than previous answer in ES5

var arr = [
   [1, 3, 5, 6, 0, 2],
   [1, 2, 7, 4, 0, 6],
   [1, 2, 3, 4, 5, 6]
 ]
 var sortIndices = arr[0].slice().sort(function(a, b) {
   return a - b;
 }).map(function(el) {
   return arr[0].indexOf(el)
 });

 arr = arr.reduce(function(a, c) {
   var sorted = sortIndices.map(function(i) {
     return c[i];
   });
   a.push(sorted);
   return a;
 }, []);

 document.getElementById('pre').innerHTML = JSON.stringify(arr)
<pre id="pre"></pre>

1 Comment

Thank you very much @charlietfl

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.