Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 sorting a second arraylist in accordance with a first arraylist
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
blackhawk_prince




PostPosted: Sun Jul 27, 2008 5:54 pm   Post subject: sorting a second arraylist in accordance with a first arraylist

my question:
I have two arraylists:
Arraylist firstList = <50, 30, 70, 20>
Arraylist secondList = <20, 65, 89, 32>
Now if I sort firstList then:
firstList = <20, 30, 50, 70>
Now I want secondList to be sorted in order with the same original index number as firstList.
secondList = <32, 65, 20, 89>
I know I explained this poorly:( but how do I code this?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Jul 27, 2008 6:22 pm   Post subject: RE:sorting a second arraylist in accordance with a first arraylist

in the sort code where you sort the first array, apply the same transitions to both arrays.

Though this is also the reason as to why parallel arrays are bad (possibility of orders going out of sync), so you might want to consider implementing a new class that holds both values.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: Mon Jul 28, 2008 8:46 am   Post subject: RE:sorting a second arraylist in accordance with a first arraylist

Tony is right in his second recommendation: you should not be writing custom sorts (outside of CS classes and high-end research, of course).

Instead, make a class that implements Comparable, then you can use the Collection.sort() or Array.sort() methods to automatically sort all the entries into the order defined by the compare() method.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: