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

  • SEARCH
  • Home
  • 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 669787
In Process

The Archive Base Latest Questions

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

I have an array that I want to sort based on the value of

  • 0

I have an array that I want to sort based on the value of one of the values in the array. The way I want to sort it is to evenly distribute the value I am searching for throughout the resulting array. For example –

Original array = [a,b,c,d,1,2,3,4]

I want to achieve – [a,1,b,2,c,3,d,4]

I can work out when to insert the numbers (total items / no. of numbers) = 2 so every 2nd item should be a number.

What is the easiest and most efficient way to do this type of sorting?

  • 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:11:21+00:00Added an answer on May 14, 2026 at 12:11 am
    var a = [a,b,c,d,1,2,3,4];
    var half = a.length / 2;
    var c = [];
    for(var i = 0; i < half; i++)
    {
      c.push(a[i]);
      c.push(a[half + i]);
    }
    a = c;
    

    General solution:
    Set r to appropriate value and make sure that array length corresponds to that value. For instance if r is 2, the ratio of number of letters to number of digits is 2:1 and hence the length of the array, len, should be a multiple of 3.

    function mix(a:Array, r:Number):void
    {
      var len1 = a.length * r / (r + 1);
      var len2 = a.length - len1;
      var c = [];
      for(var i = 0; i < len2; i++)
      {
        for(var j = 0; j < r; j++)
          c.push(a[i * r + j]);
        c.push(a[len1 + i]);
      }
      trace(c.join());
    }
    var r = 2; //two letters per one digit
    //8 letters and 4 digits
    var a = ['a','b','c','d','e','f','g','h','1','2','3','4'];
    mix(a, r)   // a,b,1,c,d,2,e,f,3,g,h,4
    
    r = 3; //three letters per one digit
    //9 letters and 3 digits
    a = ['a','b','c','d','e','f','g','h','i','1','2','3'];
    mix(a, r);  // a,b,c,1,d,e,f,2,g,h,i,3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.