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
HLRosenbergerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Big MontyWeb 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;
}
);

Open in new window

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.

Start your 7-day free trial
Big MontyWeb Ninja at largeCommented:
HLRosenbergerAuthor 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?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Big MontyWeb 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
HLRosenbergerAuthor Commented:
I'm unfamiliar with this syntax:

return a[0] < b[0] ? -1 : 1;
return a[1] < b[1] ? 1 : -1;
Big MontyWeb 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
Julian HansenCommented:
Ternary IF

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

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

Basically a shorthand if statement.
HLRosenbergerAuthor 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?
Julian HansenCommented:
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.
HLRosenbergerAuthor 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;
Big MontyWeb Ninja at largeCommented:
give it a shot and see what happens :)

but yes that should work
Julian HansenCommented:
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;
}

Open in new window

HLRosenbergerAuthor Commented:
Thanks so much!  that does the trick!
HLRosenbergerAuthor Commented:
Julian If I add that return 0, it seems to mess up the ordering.
Julian HansenCommented:
Can you post your code?
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.