By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

Need Help Sorting a JavaScript Array ... ????

P: n/a
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 ....
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 20 '05 #2

P: n/a
thanks for your assistance, I will also do some testing and post back results ..

cheers, Mark ..
Jul 20 '05 #3

P: n/a
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.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.