Computer Science Canada

sorting a second arraylist in accordance with a first arraylist

Author:  blackhawk_prince [ 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?

Author:  Tony [ 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.

Author:  DemonWasp [ 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.


: