Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 672399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:28:51+00:00 2026-05-14T00:28:51+00:00

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

  • 0

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 to have a “zig-zag” when rendering later.

// rearrange the array according to this schema
1  3  6 10

2  5  9 13

4  8 12 15

7 11 14 16

// the original array should look like this:

a = [1,5,2,9,6,3,13,10,7,4,14,11,8,15,12,16]
// the second index to draw should be the first index in the second row,
// which is represent by 5 in the original 1D Array

Yeah, now I’m looking for a smart formula 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++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:28:52+00:00Added an answer on May 14, 2026 at 12:28 am

    You can leave it sorted in the original order, you just have to step through it differently. EDIT: Turns out that my naive implementation didn’t account for the differing step size based on the diagonal. The code below does and has been tested in C#.

     var diagonals = new [] { 1, 2, 3, 4, 4, 3, 2, 1 };
     for (int i = 0, m = 0; m < 4; i = i + m, ++m) {
         for (int j = m, k = 0; k < 4; j = j + diagonals[m+k+1], ++k) {
              Console.Write( i+j+1  );
              Console.Write( " " );
         }
        Console.WriteLine();
     }
    

    Obviously, you could use this algorithm to fill a new array if you needed to keep that ordering around. It should also scale — you just need to change the termination conditions to the square root of the array size and automate the generation of the diagonals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array, $myarray[0]['first_name'] $myarray[0]['last_name'] I want to turn it into: $myarray['first_name']
I have the following problem. I have an array of bytes that I want
I'm having some trouble figuring this out. I have the following CSV string hello
I have an array of strings in .xsl file, now I have to use
I have the following form that uses some jQuery to allow an array of
I have some trouble with this. I have one database with the following tables:
I have following array, that I need to operate by hand on bitmaps. const
I have the following array. <cfset ItemHasUsers = arrayNew(1)> <cfloop query=qReadData> <cfset ItemHasUsers[qReadData.currentrow][ID] =
I have the following array as3 example: var arrayDefinitionsargsAmfPhp:Array = new Array(); arrayDefinitionsargsAmfPhp['tabela'] =
I have the following array structure: Array ( [0] => Array ( [configuration_id] =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.