In article <24bbe2a9.0312150212.333b61cb@posting.google.com >,
mark.thompson@talk21.com (Tommo) writes:
[color=blue]
>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.[/color]
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