# 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?
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.

Senior Web Developer / CEO of ExchangeTree.org Commented:
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;
}
);
``````
0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Senior Web Developer / CEO of ExchangeTree.org Commented:
0
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?
0
Senior Web Developer / CEO of ExchangeTree.org Commented:
no problem, we're here to help :)

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

return a[0] < b[0] ? -1 : 1;
return a[1] < b[1] ? 1 : -1;
0
Senior Web Developer / CEO of ExchangeTree.org Commented:
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
0
Commented:
Ternary IF

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

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

Basically a shorthand if statement.
0
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?
0
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.
0
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;
0
Senior Web Developer / CEO of ExchangeTree.org Commented:
give it a shot and see what happens :)

but yes that should work
0
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;
}
``````
0
Author Commented:
Thanks so much!  that does the trick!
0
Author Commented:
Julian If I add that return 0, it seems to mess up the ordering.
0
Commented:
Can you post your code?
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.