0

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;}


        }

    }
}

}

  • Getting the highest and lowest entries is not the same as sorting the array. You may want to clarify in your question what you are needing. – Tony B Jul 12 '23 at 13:45

1 Answers1

0
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;

                }
            }

        }
    }
}
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 14 '23 at 17:06