Formula needed: Sort Array

Posted by aw on Stack Overflow See other posts from Stack Overflow or by aw
Published on 2010-03-17T11:41:51Z Indexed on 2010/03/17 12:01 UTC
Read the original article Hit count: 301

Filed under:
|
|
|
|

I have the following array:

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

I use it for some visual stuff like this:

1   2  3  4
5 6 7 8
9 10 11 12
13 14 15 16

Now I want to sort the array like this:

1  3  6 10
2 5 9 13
4 8 12 15
7 11 14 16
//So the original array should look like this: a = [1,5,2,9,6,3,13,10,7,4,14,11,8,15,12,16]

Yeah, now I'm looking for a smart formula to do that

ticker = 0;
originalArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
newArray = [];

while(ticker < originalArray.length)
{
    //do the magic here
    ticker++;
}

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about arrays