rahul888
rahul888

Reputation: 413

Array printing in a specific order

I have 3 arrays say A, B, C A has a1,a2,a3 similarly b1,b2,b3 for B and C.

When I print their elements, they come like this

return(A)-->a1,a2,a3
return(B)-->b1,b2,b3
return(C)-->c1,c2,c3

How could I manipulate these array to another array say D to print something like this

return(D)-->
a1,b1,c1,a2,b2,c2,a3,b3,c3

I use javascript .

Upvotes: 2

Views: 988

Answers (4)

Moritz Roessler
Moritz Roessler

Reputation: 8621

If you just need to concatenate all Arrays, you could write a simple helper function, that makes use of Array.prototype.reduce and Array.prototype.concat

function concatAll () {
  return [].reduce.call (arguments, function (a,b) {return a.concat (b)},[])
}

To use it, simply call it with all arrays you want to concatenate.

var a = ["a1", "a2", "a3"],
    b = ["b1", "b2", "b3"],
    c = ["c1", "c2", "c3"];

concatAll (a,b,c) //["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"] 

If you need to sort you array after that too, Array.prototype.sort takes a function as parameter which you can use to sort after your numerical value first, by putting a weight on it.

concatAll (a,b,c).sort(function (a, b) {
    var aVals = a.match(/(\D*)(\d*)/),
        bVals = b.match(/(\D*)(\d*)/),
        weighted = [a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0, b[1] - a[1]] //[string,number]

    return weighted[0] - 2 * weighted[1] // give the number a double weight

}) //["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]

Heres an example on jsFiddle

Upvotes: 0

UltraInstinct
UltraInstinct

Reputation: 44444

What you are trying to do is called a zip operation. Javascript does not have zip by default. I wrote a small zip function which might work for you, what it expects is that all arrays have the same length. To be a bit more precise, it would want all the arrays to have atleast as many elements as are there in element #1. This essentially does what @Igle's code does, except that it uses a bit different approach and can take any number of arrays.

Note: You might need to do a bit of error checking in below code

var zip = function (array) {
   return array.slice(1).reduce(function(prev,cur) {
       return prev.map(function(x,i){
           return x.concat(cur[i]);
       });
   }, array[0].map(function(x){return [x];}));
}

zip ([ ['a1','a2','a3'] ,  ['b1','b2','b3'],  ['c1','c2','c3'] ]);

Outputs:

[["a1","b1","c1"],["a2","b2","c2"],["a3","b3","c3"]]

UPDATE:

I found another cleaner implementation of zip as compared to the above. Check it out here: Javascript equivalent of Python's zip function

Upvotes: 1

Igl3
Igl3

Reputation: 5108

You could write a loop for that if they all have the same length like this:

var D = [];

for(var i = 0; i<A.length; i++){
  D.push(A[i],B[i],C[i]);
}

return D;

Upvotes: 3

Florent
Florent

Reputation: 12420

First you need to concat the arrays together:

var D = A.concat(B, C);

Then apply the desired sort:

D.sort(function(a, b) {
  return // ...
});

If you call sort() without arguments, D will be alphabetically sorted.

Upvotes: 0

Related Questions