Dr. Croly\'s beverage cellar needs to be organized. He wants a program that can
ID: 3779641 • Letter: D
Question
Explanation / Answer
public class SortingApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
String[] data = {
"The Bruery - The Smokey and the Boise: 3",
"Brasserie De Silly - Scotch Silly, Burgundy Bbl. Aged: 6",
"Brouwerij Van Steenberge - de Garre: 4",
"Jolly Pumpkin Artisan Ales - Oro de Calabaza: 1",
"Collective Brewing Project - Americal Sour Red: 2",
"Boon Brewery -2012 Geuze Mariage Parfait: 5"
};
String[] sortedData = orderData(data);
for (String sortedData1 : sortedData) {
System.out.println(sortedData1);
}
}
public static String[] orderData(String[] inData)
{
//Split the string into two and obtain the quantity & position
String[][] tempData = new String[inData.length][2];
int[][] processData = new int[inData.length][2];
for(int i = 0; i < inData.length; i++){
tempData[i] = inData[i].split(":");
tempData[i][1] = tempData[i][1].trim();
//System.out.println("tempData = " + tempData[i][0] + " -> " + tempData[i][1]);
processData[i][0] = Integer.parseInt(tempData[i][1]);
processData[i][1] = i;
}
//Sort quantity
int k;
for(int m = inData.length; m >= 0; m--){
for (int i = 0; i < inData.length - 1; i++){
k = i+1;
if(processData[i][0] < processData[k][0]){
int temp[] = new int[2];
temp[0] = processData[i][0];
temp[1] = processData[i][1];
processData[i][0] = processData[k][0];
processData[i][1] = processData[k][1];
processData[k][0] = temp[0];
processData[k][1] = temp[1];
}
}
}
//Store the Data in temp string array
String[] tempString = new String[inData.length];
System.arraycopy(inData, 0, tempString, 0, inData.length);
//Get the sorted Data
for(int i = 0; i < tempString.length; i++){
int position = processData[i][1];
inData[i] = tempString[position];
}
return inData;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.