简体   繁体   中英

Sort a parallel array using Arrays.sort()

Is it possible to sort an array using Arrays.sort() and thereafter have another related array positioned the same as the sorted array for example:

    String arrNames[] = new String[5];
    String arrCellNo[] = new String[arrNames.length];


    String arrNamesSorted[] = new String[arrNames.length];
    System.arraycopy(arrNames, 0, arrNamesSorted, 0, arrNames.length);
    Arrays.sort(arrNamesSorted);

From this point what i would like to do is sort the CellNo array such that if "person" had a cellNo "x", he will have the same "cellNo" "x" after the array arrNames is sorted

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.

I would go for a different approach:

  1. Create a new object:

     public class Person { private name; private cellNo; // Implement getters and setters } 
  2. Create a comparator:

     public MyComparator implements Comparator<Person> { public int compare(Person a, Person b) { return a.getName().compareTo(b.getName()); } } 
  3. Call Array.sort(persons, new MyComparator()) on a Person[] persons = ... array

If names will be unique, consider using a SortedMap :

final SortedMap<String,String> nameToCellNo = new TreeMap<>();
for (int i = 0; i < arrNames.length; i++) {
  nameToCellNo.put(arrNames[i], arrCellNo[i]);
}
int ctr = 0;
for (Map.Entry<String,String> entry : nameToCellNo.entrySet()) {
  arrNamesSorted[ctr] = entry.getKey();
  arrCellNoSorted[ctr++] = entry.getValue();
}

I found some of the concepts introduced in the answers hard to grasp therefore in my own solution resorted to undesirable programming methods as a trade of for code easier to understand and created a bubble sort method and at the end manipulated the second array like so :

String arrNames[] = new String[5];
String arrCellNo[] = new String[arrNames.length];
String arrNamesSorted[] = new String[arrNames.length];
String arrCellNoSorted[] = new String[arrCellNo.length];

System.arraycopy(arrNames, 0, arrNamesSorted, 0, arrNames.length);
System.arraycopy(arrCellNo, 0, arrCellNoSorted, 0, arrCellNo.length);

for (int i = 0; i < arrNamesSorted.length; i++) {
    for (int j = 0; j <arrNamesSorted.length-1; j++) {
        if (arrNamesSorted[j].compareTo( arrNames[j+1])>0) {
            String temp = arrNamesSorted[i];
            arrNamesSorted[i] = arrNamesSorted[j];
            arrCellNoSorted[i] = arrCellNoSorted[j];
            arrNames[j] = temp;
            }
         }
   }

It is possible to use the built-in Arrays.sort to archive the effect without creating a class for the parallel array content.

Note that the index should be an object array , not primitive array . ( Arrays.sort(int[]) does not take comparator)

final int n = 10;
int[] values = new int[n];
Integer[] index = new Integer[n];
par_foreach(n, i -> index[i] = i);
par_foreach(n, i -> values[i] = random.nextInt(100));
Arrays.sort(index, (a, b) -> Integer.compare(values[a], values[b]));
println("values", values);
println("index", index);
print("ordered:");
foreach(n, i -> print(" " + values[index[i]]));
println();

Remark

foreach :: Num -> (Num -> void) (parallel) par_foreach :: Num -> (Num -> void) In case you cannot imagine the implementation: https://github.com/beenotung/javalib/blob/master/src/com/github/beenotung/javalib/Utils.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM