469,934 Members | 1,847 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Sorting a two dim array

Hi,

I have an array as follows -

var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);

I've tried randomizing this array using the following but its obviously
wrong -

for (i=0;i<noOfVerbs;i++) {

MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);

}

function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}

I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.

How can I do this?

Thanks,

Barry.

Jul 23 '05 #1
4 5176
JRS: In article <11*********************@g47g2000cwa.googlegroups. com>,
dated Fri, 8 Jul 2005 10:29:50, seen in news:comp.lang.javascript,
bg***@yahoo.com posted :
I have an array as follows -

var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);

I've tried randomizing this array using the following but its obviously
wrong -

for (i=0;i<noOfVerbs;i++) {

MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);

}

function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}

I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.

How can I do this?


Rather a confusing description.

You should not randomise by random sorting; there are better, easy
methods. FAQ 4.22 has a link on shuffling.

The .sort routine is written in the expectation that the result of
randomSort(w1,w2) depends on, and only on, w1,w2, reproducibly. FAQ.

For your purpose, if I understand it, which I doubt, you should shuffle
the outer 1D array, in which each entry is itself an array. That means
either redesigning your algorithm to use the indexes in reverse order,
or transposing, shuffling, and re-transposing.

Another approach would be to deal - FAQ 4.22 has a link on dealing -
integers into an array of size num, then to copy your 2D array with the
position of each element in the new sub-arrays given by lookup in the
once-dealt array.

You could write your own random function, initialisable by yourself, so
that after each initialisation it would generate the same sequence, thus
being able to random-sort each sub-array identically. But not a good
idea. FAQ.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #2
Lee
bg***@yahoo.com said:

Hi,

I have an array as follows -

var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);

I've tried randomizing this array using the following but its obviously
wrong -

for (i=0;i<noOfVerbs;i++) {

MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);

}

function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}

I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.

How can I do this?


If I understand what you want, each element of MultiArray[0]
should still be lined up with the corresponding element of
MultiArray[1] after both arrays have been sorted.

That's an excellent indication that you should really be
using an array of custom Objects, each with two fields.

for example, if MultiArray[0] is a list of names, and
MultiArray[1] is the id number that goes with those names:

var person = [
{name:"Jim", id:"0123"},
{name:"Sue", id:"4321"},
{name:"Bob", id:"9331"}
];

Then you can sort or shuffle this single array and keep the
two fields together. It also has the advantage that the
variable names can be much more meaningfull than
"MultiArray[1][3]" (which would be referenced as "person[3].id",
in my example).

Jul 23 '05 #3

Why the shuffling anyway? I gather you want to output certain array
element in so-called random fashion? For that, I just use Math.random()
times a base10 amount and round it, to make it Integer and then get its
modulus against the .lenght of it,
ARRNAME[(Math.round(Math.random()*1000))%ARRNAME.length] or so.

Danny

On Fri, 08 Jul 2005 10:29:50 -0700, <bg***@yahoo.com> wrote:
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #4
Lee
Danny said:


Why the shuffling anyway? I gather you want to output certain array
element in so-called random fashion? For that, I just use Math.random()
times a base10 amount and round it, to make it Integer and then get its
modulus against the .lenght of it,
ARRNAME[(Math.round(Math.random()*1000))%ARRNAME.length] or so.


I'm just really being a pest, tonight.
Don't let me discourage you too much.

That's not a good way to generate random numbers.
Unless the length divides evenly into 1000, some of your elements
will be selected more often than others. If the multiplier is
very large, relative to the array length, the difference may
not be enough to worry about, but 1000 is not very large.

ARRNAME[Math.floor(Math.random()*ARRNAME.length)]

will select the elements with an even distribution.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Paul Kirby | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
3 posts views Thread by SilverWolf | last post: by
7 posts views Thread by Foodbank | last post: by
7 posts views Thread by Kamal | last post: by
5 posts views Thread by lemlimlee | last post: by
5 posts views Thread by jrod11 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.