473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 5303
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.