473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sorting 1 array based on another array using sort()

I have 2 arrays that are related. The first array is an array of distances from a certain spot. The second array is an array of city names. They are related in that the order of the distances array is the same initial order as the city array.

I want to sort the distance array, which I know is easy enough using the sort() method. The part I can't do is sorting the city array according to the distance array.

For example:
arraydistance = [43, 934, 831]
arraycity = [la, sandiego, orangecounty]


After the sort it should look like:
arraydistance = [43, 831, 934]
arraycity = [la, orangecounty, sandiego]


It might be cleaner or easier to put them into the same array, but I still wouldn't know how to sort. Any advice would be much appreciated!
Thanks
May 20 '07 #1
1 3189
mrhoo
428 256MB
var milage=[];var L=arraydistance.length;
for(var i=0;i<L;i++){
milage[i]=[arraycity[i],arraydistance[i]];
}

var distanceSort=milage.sort(function(a,b){ //sort by distance
return a[1]-b[1];
})

var citySort=milage.sort(function(a,b){ //sort by name
a=a[0].toLowerCase();
b=b[0].toLowerCase();
if(a==b) return 0;
if(a>b) return 1;
return -1;
})

for(var i=0;i<l;i++){
arraycity[i]=milage[i][0];
arraydistance[i]=milage[i][1];
}
May 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: LRW | last post by:
Subject line confusing? The whole thing is confusing to me. OK, I think the root of my problem, aside from mental, is array based...but I'm not even sure what I want to do is even possible. So if...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
4
by: stelladiary2004 | last post by:
Hi! I would appreciate some help with this (basic) question. I need to copy an 2D array x to another 2D array y. I know how to do it copying element by element, but I would like to use a faster...
13
by: webb | last post by:
I have a table with a huge amount of data. At present my data is like so. userid:1588 | 1 | CR1 userid:1588 | 2 | CR2 userid:1588 | 1 | CR3 userid:1588 | 1 | CR4 userid:1588 | 1 | CR5...
1
by: jeric | last post by:
I have two byte arrays, X and Y. X contains 100 elements, and Y has 10 elements. I want Y to equal X through X. How can I do this without a loop? While in this example the arrays are only...
1
by: agendum97 | last post by:
MSDN says splice has the following arguments: arrayObj.splice(start, deleteCount, ]]]) Thus I can insert items into an array using splice. But how do I insert an entire array? For example:...
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
9
by: manojrana | last post by:
Can we store an array within another array in javascript var locat = new Array(); var element = new Array(); locat = element; or Can we use like this locat = "manoj";
1
by: ekailan | last post by:
wchar_t a; wchar_t b; a=L"asdfgh"; why I cant say b=a; How to exchange values between tow wchar_t arrays
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.