1. Registered User
Join Date
Mar 2010
Posts
13

## Multiple Array Sorting

I have two arrays that coincide with each other. One array has the name and the other has a numerical value that goes with the name.

Ex:
name = [name1, name2, name3]
value = [12,11,17]

So, name1 = 12, name2 = 11, and name3 = 17.

What I want to do is reorder the value array from greatest to least, but also reorder the name array so that the values and names still line up.

Ex result:
name = [name3, name1, name2]
value = [17,12,11]

I cannot find a way to do this. I know how to reorder an array, just not how to mirror the order in a separate array.

Chris

2. Registered User
Join Date
Mar 2010
Posts
13

## Still Trying

Maybe what I asked for isn't possible. Here's a different way I could go about it also. Is there a way to sort nested arrays?

HTML Code:
```<script type="text/javascript">

function sortNumber(a, b)
{
return a - b;
}

var n = [10, "winona"];
var m = [14, "alab"];
var l = [13, "greg"];

var c = [n,m,l];

document.write(c[][0].sort(sortNumber));

</script>```
This doesn't work, but is there a way I can sort the arrays nm&l based on the numbers in the 0 columns of their arrays?

Any suggestions are welcome. Thanks again.

3. Why not to try to combine both arrays into one "two dimensional" array and sort the latter according to your preferences?

4. Registered User
Join Date
Mar 2010
Posts
13
Isn't that what the code I have above is doing? Is 'var c' a two dimensional array? I still can't figure out how to sort that kind of array numerically.
Last edited by cdoor84; 04-02-2010 at 03:36 PM.

5. Registered User
Join Date
Mar 2010
Posts
13

## closer...

This is as close as I have gotten. It works alphabetically only however, so if for example you change 152 to 15, it no longer is in numerical order. Any suggestions?

HTML Code:
```<script type="text/javascript">

function compare(a, b) {
if (a < b) {
return 1;
}
if (a > b) {
return -1;
}
if (a == b) {
return 0;
}
}

var n = [150, "winona"];
var m = [142, "mlab"];
var l = [152, "greg"];

var c = [n,m,l];

document.write(c.sort(compare));
</script>```

6. Well I guess you are right... Here is what I have tried to illustrate the idea:

Code:
```myName = ["name1", "name2", "name3"];
myValue = [12,11,17];

myNameValue = new Array();;

for (i=0; i<myName.length; i++){
myNameValue[i] = [myName[i], myValue[i]];
}

function sortByRelevance(a,b) {
a = a[0];
b = b[0];
return a == b ? 0 : (a > b ? -1 : 1)
}

myNameValue.sort(sortByRelevance);

alert (myNameValue[0][0] + ", " + myNameValue[0][1]);```
I reversed the array sorting order function for the rearrangement to be obvious.
To sort in the right order, use this instead:

return a == b ? 0 : (a > b ? -1 : 1)

7. Registered User
Join Date
Mar 2010
Posts
13
Nice, i switched the order (value before name) so its sorts the value and it seems to be working so far. This must be the main step I was messing up:

function sortByRelevance(a,b) {
a = a[0];
b = b[0];

return a == b ? 0 : (a > b ? -1 : 1)
}

Thanks a ton for your help!

8. You are welcome! When you get the principle, everything else is up to your customization. If it interests you, you can read how to sort these 2-d arrays here:
Especially how Nedals explained this issue to me.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•