Sort a parallel array using Arrays.sort()

You can’t have Arrays.sort manipulate a second array the way it’s sorting the first array.

The solution is to sort your own objects that contain all the data you need. Create a Contact class with name and cell number attributes. Then create a class that implements Comparator<Contact> (say, ContactComparator) to compare the names.

Then you will be able to sort an array of Contact objects with a particular overload of Arrays.sort.

Arrays.sort(arrContacts, new ContactComparator());

All data will remain organized, in that the same name will still have the same cell number.

Leave a Comment