1

With this array:

var arr = [];
arr[0] = [1, 'Peter', 3];
arr[1] = [1, 'Mary', 2];
arr[2] = [0, 'David', 5];
arr[3] = [0, 'John', 4];
arr[4] = [0, 'Billy', 1];

This works fine:

arr.sort(function (a,b) {
    console.log(a[2]);

    if (a[2] > b[2]) return  1;
    if (a[2] < b[2]) return -1;
    return 0;
});     

But with an array like this:

var arr = [];
arr[0] = [1, 1, 0, 0, 0];
arr[1] = ['Peter', 'Mary', 'David', 'John', 'Billy'];
arr[2] = [3, 2, 5, 4, 1];

A[2] gets 0-David.

I returned everything, I really cannot figure it out. Please, do you know how I could sort the second array according to arr[2] list?

4
  • 3
    You might be better off mapping it to the first and than converting it back to the second after the sort. Commented Apr 24, 2017 at 13:33
  • One thing to keep in mind is that in the first case you are sorting the array arr and in the second you are sorting a mash of the subarrays, so arr.sort will never do the trick because you are not trying to sort arr. As @epascarello said, you will be better mapping one to the other. Commented Apr 24, 2017 at 13:35
  • 1
    So, if I understand the problem correctly, you're trying to sort array1 and array2 based on the values of array3. This is not trivially implemented, and I suppose you're trying to do something that can be way easier if you didn't want to do it this way. I can suggest you transpose your matrix, sort it with the first implementation, then transpose it back. Or you can implement a very special sort, but by using the .sort() it's not really possible. Commented Apr 24, 2017 at 13:36
  • Thank you I appreciated your advice. I have distributed the table at the base. It is fascinating to see such a dynamic and voluntary community. Thank you all! Commented Apr 24, 2017 at 17:56

2 Answers 2

1

You could use another array with the indices, sort them as desired and map the result to the given array.

var array = [[1, 1, 0, 0, 0], ['Peter', 'Mary', 'David', 'John', 'Billy'], [3, 2, 5, 4, 1]],
    sortBy = array[2],
    indices = sortBy.map(function (_, i) { return i; });

indices.sort(function (a, b) { return sortBy[a] - sortBy[b]; });
array = array.map(function (a) {
    return indices.map(function (i) { return a[i]; });
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

Here's an ugly implementation for what you want to achieve, however, I feel that you're overcomplexifying something simple with this approach.

What I suggest is that you transpose your matrix, sort it with your first implementation, then transpose it back to the original layout.

var arr = [];
arr[0] = [1, 1, 0, 0, 0];
arr[1] = ['Peter', 'Mary', 'David', 'John', 'Billy'];
arr[2] = [3, 2, 5, 4, 1];

function transpose(array) {
    return array[0].map(function(col, i) {
        return array.map(function(row) {
            return row[i]
        })
    });
}

function twistedSort(matrix, sortingRowIndex) {
    var transposed = transpose(matrix);

    transposed.sort(function(a, b) {
        if (a[sortingRowIndex] > b[sortingRowIndex]) return 1;
        if (a[sortingRowIndex] < b[sortingRowIndex]) return -1;
        return 0;
    });

    return transpose(transposed);
}

twistedSort(arr, 2);

Once again, I suggest rethinking your problem, but if you're sure you need this complex of a solution for this problem, then here you go :)

Comments

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.