How to sort a parallel array to get the greatest and smallest value

31 Views Asked by At

I have two parallel arrays. one is type String and the other, type int. I want to sort them in ascending order while maintaining data consistency, keeping them parallel, to get the entry with the greatest int value and the entry with the int lowest value.

package sortparallelarray;

import javax.swing.JOptionPane;

public class SortParallelArray {

static String product[] = {"chips", "sweets", "juice", "cookies", "ice cream", "chocolate"};
static int price[] = {5, 3, 7, 6, 9, 8};

public static void main(String[] args) {
    // the elements are displayed in the order they were hard coded, unsorted. 
    displayArrays(formatDisplay());
    
   
}
public static void displayArrays(String displayable) {
    JOptionPane.showMessageDialog(null,displayable);
}
public static String formatDisplay() {
    String result="";
 for (int i = 0; i < price.length; i++) {
     result += "Product : "+product[i] +" Price : R" +price[i]+"\n";
 }
 return result;
}
public static void sortArraysInAscendingNumericalOrder() { 

            }
 public static String getGreatestElementInPriceArray(){
 String greatestElement = "Most expensive product\n\n";
 sortArraysInAscendingNumericalOrder(); 
 return greatestElement;}

 public static String getSmallestElementInPriceArray(){
 String SmallestElement = "Cheapest pruduct\n\n";
 
 return SmallestElement;}


        }

    }
}

}

1

There are 1 best solutions below

1
On
package sortparallelarray;

import javax.swing.JOptionPane;

public class SortParallelArray {

    static String product[] = {"chips", "sweets", "juice", "cookies", "ice cream", "chocolate"};
    static int price[] = {5, 3, 7, 6, 9, 8};

    public static void main(String[] args) {
        // the elements are displayed in the order they were hard coded, unsorted. 
        displayArrays(formatDisplay());

        displayArrays(getGreatestElementInPriceArray());
        displayArrays(getSmallestElementInPriceArray());

        // note that the second time all elements are displayed, they are sorted. this is because the sort method is called in the two methods above.
        displayArrays(formatDisplay());

    }

    public static void displayArrays(String displayable) {
        JOptionPane.showMessageDialog(null, displayable);
    }

    public static String formatDisplay() {
        String result = "";
        for (int i = 0; i < price.length; i++) {
            result += "Product : " + product[i] + " Price : R" + price[i] + "\n";
        }
        return result;
    }

    public static String getGreatestElementInPriceArray() {
        String greatestElement = "Most expensive product\n\n";
        sortArraysInAscendingNumericalOrder(); // the sort method is called in order for the greatest value to be placed at the last index
        greatestElement += "Product : " + product[product.length - 1] + " Price : R" + price[price.length - 1] + "\n";

        return greatestElement;
    }

    public static String getSmallestElementInPriceArray() {
        String SmallestElement = "Cheapest product\n\n";
        sortArraysInAscendingNumericalOrder();// the sort method is called in order for the smallest value to be placed at the first index
        SmallestElement += "Product : " + product[0] + " Price : R" + price[0] + "\n";

        return SmallestElement;
    }

    public static void sortArraysInAscendingNumericalOrder() {
// bubble sort to arrange the arrays in Ascending numerical order according to the price array
        for (int i = 0; i < price.length; i++) {
            for (int j = 0; j < price.length - (i + 1); j++) {
                if (price[j] > price[j + 1]) {
                    int tempPrice = price[j];
                    price[j] = price[j + 1];
                    price[j + 1] = tempPrice;

                    String tempProduct = product[j];
                    product[j] = product[j + 1];
                    product[j + 1] = tempProduct;

                }
            }

        }
    }
}