Sort a parallel array using Arrays.sort()

前端 未结 5 1469
暗喜
暗喜 2020-11-30 15:41

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:



        
5条回答
  •  半阙折子戏
    2020-11-30 16:37

    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 0) {
                String temp = arrNamesSorted[i];
                arrNamesSorted[i] = arrNamesSorted[j];
                arrCellNoSorted[i] = arrCellNoSorted[j];
                arrNames[j] = temp;
                }
             }
       }
    

提交回复
热议问题