# Javascript; arrays, sorting

I have numbers (mileage) that I am sorting in an array using  .sort();  Each number has associated text in a second array.    What "trick" could I use to keep association between the number and it's text after I have sorted the first array?
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Web Ninja at largeCommented:
use a two dimensional array, it'll be a lot easier to manage:

``````myarr = [
['text for mileage1', 17],
['text for mileage2', 48],
['text for mileage3', 36],
['text for mileage4', 18]
];

myarr.sort(
function(a,b) {
if (a[1] == b[1])
return a[0] < b[0] ? -1 : 1;
return a[1] < b[1] ? 1 : -1;
}
);
``````

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Web Ninja at largeCommented:
Author Commented:
ok, thanks.   I not a javascript expert, but in your example, you are doing the sort?   Can I have a 2 dimensional  array and then sort it using one of the dimensions?
Web Ninja at largeCommented:
no problem, we're here to help :)

the example I gave uses a two dimensional array and sorts on the second column
Author Commented:
I'm unfamiliar with this syntax:

return a[0] < b[0] ? -1 : 1;
return a[1] < b[1] ? 1 : -1;
Web Ninja at largeCommented:
think of it as a condensed IF statement, it's essentially the same as:

if( a[0] < b[0] )
return -1
else
return 1

because the first RETURN line is part of the if statement above it, if that condition isn't met, then the 2nd return line fires off
Commented:
Ternary IF

x = (condition) ? (statement if true) : (statement if false);

https://en.wikipedia.org/wiki/%3F:

Basically a shorthand if statement.
Author Commented:
So, that little bit of code is doing the sort?   I know there is a .sort(); method for an array.  Does that only apply to 1 dimensional arrays?
Commented:
So, that little bit of code is doing the sort?

The code inside .sort is doing the sort - it is calling that bit of code to help it make a decision when given to elements which one is bigger (in terms of sorting) than the other. It takes the result of that piece of code and uses it to arrange the two elements it is comaparing.
Author Commented:
ok.  looks like it is sorting descending.  I need ascending, so I can just change it to this, correct:

return a[0] > b[0] ? -1 : 1;
return a[1] > b[1] ? 1 : -1;
Web Ninja at largeCommented:
give it a shot and see what happens :)

but yes that should work
Commented:
In theory the function should return 1 of three values - there should be a comparison for equality.

Strictly speaking your function should be this
``````function(a,b) {
if (a[1] == b[1]) {
if a[0] == b[0]) return 0;
return a[0] > b[0] ? -1 : 1;
}
return a[1] > b[1] ? 1 : -1;
}
``````
Author Commented:
Thanks so much!  that does the trick!
Author Commented:
Julian If I add that return 0, it seems to mess up the ordering.
Commented: