Hello All,
whilst I know how to sort an array I am having problems with
my situation, what I would ideally like is something like a 'sort
keys' action with Perl hashes.
I am reading in data and building an array entry that will
be of the form:-
array[0] - horse1,2,4,6,1,3,6 = 22
...
array[6] - horse6,2,3,6,1,9,8 = 29
etc etc.
I know want to sort these array entries based on the totals (i.e 22,
29 etc), I figured you cannot do this in one array so maybe 2 arrays,
one array would be as follows:-
array1[0] - horse1,2,4,6,1,3,6 =
...
array1[6] - horse6,2,3,6,1,9,8 =
and the second array:-
array2[0] - 22
...
array2[6] - 29
I could then sort on the 2nd array, the trouble is that I loose the
relationship between the 2 arrays.
I thought about creating an object array to sort upon but am unsure if
this would work. Has anyone a solution to this and if so some example
code to help me along.
thanx, Mark .... 3 1392
In article <24**************************@posting.google.com >, ma***********@talk21.com (Tommo) writes: Hello All, whilst I know how to sort an array I am having problems with my situation, what I would ideally like is something like a 'sort keys' action with Perl hashes. I am reading in data and building an array entry that will be of the form:- array[0] - horse1,2,4,6,1,3,6 = 22 ... array[6] - horse6,2,3,6,1,9,8 = 29 etc etc.
Making the assumption, based on your above data, that array[X] will not always
correspond to horseX since your array[6] is horse6 but array[0] is not horse0.
Anyway, its not relevant for this approach.
is the array created as a string, or as seperate entries?
myArray = new Array()
myArray[0] = new Array('horse0',2,4,6,1,3,6)
//more data
myArray[6] = new Array('horse6',2,3,6,1,9,8)
//more efficient to use bracket notation:
myArray = new Array()
myArray[0] = ['horse0',2,4,6,1,3,6]
//more data
myArray[6] = ['horse6',2,3,6,1,9,8]
//although I personally never use this approach, its too confusing sometimes.
myArray2 = new Array()
for (i=0;i<myArray.length;i++)
{
myArray2[i] = new Array()
//lets save the reference
myArray2[i][1] = myArray[i][0]
k = 0;
//lets total the rest of it
for (j=0;j<myArray[i].length;j++)
{
k = k + (+myArray[i][j]);
}
myArray2[i][0] = k;
}
myArray2.sort()
//In my personal experience, when you sort an array that is an array of arrays,
it sorts on the first element in the secondary array
Untested. How you would go about it depends a lot on how your original array is
constructed though. If the original array is constructed as a plain string,
then you would have to split it on the delimiter. It might be more efficient to
construct it as a hash array and go from there. Needs testing. Its 7:30AM here
and no time to test it. Will test it further later today.
--
Randy
thanks for your assistance, I will also do some testing and post back results ..
cheers, Mark ..
JRS: In article <24**************************@posting.google.com >, seen
in news:comp.lang.javascript, Tommo <ma***********@talk21.com> posted at
Mon, 15 Dec 2003 02:12:27 :- whilst I know how to sort an array I am having problems with my situation, what I would ideally like is something like a 'sort keys' action with Perl hashes. I am reading in data and building an array entry that will be of the form:- array[0] - horse1,2,4,6,1,3,6 = 22 ... array[6] - horse6,2,3,6,1,9,8 = 29 etc etc.
You can use the sort method of the outer array by providing it with a
suitable compare function.
function Compare(X, Y) { var SX = SY = 0
for (J=1; J<X.length; J++) SX += X[J] // form score
for (J=1; J<Y.length; J++) SY += Y[J] // form score
return SY - SX }
That is probably about right; it should return the difference between
the scores of the two horses. If X.length = Y.length always, the loops
can merge; otherwise, the job seems unfair.
For efficiency, though, the sums should be calculated once per horse;
and, as someone said, if they are placed at the start of the data
structure the default sort could suffice.
To use sort keys in general, just provide a compare function that
compares them appropriately, returning <0, 0, >0 accordingly.
Consider representing each animal as
Nag = {Score:22, Name:"Shergar", Data:[2,4,6,1,3,6]}
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Derek |
last post by:
Hi,
I've built a rather large CGI that dumps a lot of data and a fairly
complex javascript app out to the client's browser. Granted this may
be poor style according to someone web design...
|
by: johkar |
last post by:
I am in the process of editing the below code I found online, I have a
from multi-select list and a to multi-select list. Before rewriting
the to list, I want to sort it but ignore the "F - " and...
|
by: Bob |
last post by:
Sorting the following alphanumerics using myArray.sort():
04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001
Results in:
|
by: bg_ie |
last post by:
Hi,
I have an array as follows -
var MultiArray = new Array(2);
MultiArray = new Array(num);
MultiArray = new Array(num);
I've tried randomizing this array using the following but its...
|
by: mike |
last post by:
If I had a date in the format "01-Jan-05" it does not sort properly
with my sort routine:
function compareDate(a,b)
{
var date_a = new Date(a);
var date_b = new Date(b);
if (date_a < date_b)...
|
by: rrowles2000 |
last post by:
Hi,
I have four chuncks of html and I'm trying to sort them. see this
link. Basically the sort works for alphabetic sort but not for
numerics. Any ideas very welcome? I just can't see what I'm...
|
by: Totti |
last post by:
Hi everyone,
I am a newbie to javascript learning it for 2 months now, i am trying
to make a sorter who will read english words from the one textbox or
datafile and store them in a 2 dimensional...
|
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...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |