Formula needed: Sort array to array-"snaked"
Posted
by aw
on Stack Overflow
See other posts from Stack Overflow
or by aw
Published on 2010-03-17T17:36:11Z
Indexed on
2010/03/18
9:51 UTC
Read the original article
Hit count: 426
After the you guys helped me out so gracefully last time, here is another tricky array sorter for you.
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 and render it 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 to have a "snake" later:
// rearrange the array according to this schema 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 // the original array should look like this a = [1,2,3,4,12,13,14,5,11,16,15,6,10,9,8,7]
Now I'm looking for a smart formula / smart loop to do that
ticker = 0; rows = 4; // can be n cols = 4; // can be n 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++; }
Thanks again for the help.
© Stack Overflow or respective owner