coldfusioncfml

sort array based on sorting of a different array


I have 2 arrays:

firstArr = ["Pizza", "Pasta", "Chicken Soup", "French Fries"]
secondArr = [8, 6, 6, 9]

now I sort the second Array like this:

secondArr = [9, 8, 6, 6]

I then want the first Array to follow this new order:

firstArr = ["French Fries", "Pizza", "Chicken Soup", "Pasta"]

How can I achieve this?


Solution

  • int temp = 0;
    String temp1 = "";
    for(int i=0; i< secondArr.size(); i++) {
       for(int j=i+1; j<secondArr.size(); j++) {
          if(secondArr[i] < secondArr[j]) {
             temp = secondArr[i];
             secondArr[i] = secondArr[j];
             secondArr[j] = temp;
             // To swap the second array if first is swapped on the same index
             temp1 = firstArr[i];
             firstArr[i] = firstArr[j];
             firstArr[j] = temp1; 
          }
       }
    }