sorting between multiple arrays.
I thought that I had posted this yesterday; however as I can not find my post...
I need assistance with part of a project where I need to sort between multiple arrays; using one of the columns as the sort criteria. The arrays contain integers, strings, and doubles; however I would like to sort and display the arrays alphabetically using the string column. I was told that a bubble array may work, however as I can not find specific information on the use of a bubble array, I really don't know where to start. The arrays look like the following:
productArray[0] = new Product(1, "binder", 15, 1.5, 0, 0);
productArray[1] = new Product(2, "marker", 13, .5, 0, 0);
productArray[2] = new Product(3, "paper", 24, .95, 0, 0);
productArray[3] = new Product(4, "pen ", 5, .25, 0, 0); \
Any assistance that anyone could provide would be greatly appreciated. I am a newbie when it comes to Java and the course materials are extremely vague.
Dman
[988 byte] By [
Dman1a] at [2007-11-27 9:57:57]

Thank you for your assistance.
The site that I found to be most helpful was the http://www.onjava.com/lpt/a/3286 site; however I am still experiencing problems. I have added code to the program as noted below:
public class Inventoryprogrampart3
{
/** Creates a new instance of Main */
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// create Scanner to obtain input from command window
java.util.Scanner input = new java.util.Scanner( System.in );
double totalInventoryValue = 0;
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(); // Displays a blank line
System.out.println( "Welcome to the Inventory Program - Part 2" ); // Display the string
System.out.println( "--" ); // displays a line of characters
System.out.println(); // Displays a blank line
System.out.println( "This program will output an office supply inventory" ); // Display the string
System.out.println( "listing that includes item numbers for the" ); // Display the string.
System.out.println( "inventoried products, the items product names, the" ); // Display the string.
System.out.println( "quantity of each product in stock, the unit price" ); // Display the string
System.out.println( "for each product, the total value of each products" ); // Display the string
System.out.println( "inventory, and a total value of the entire inventory." ); // Display the string
System.out.println(); // Displays a blank line
System.out.println( "*****************************************************" ); // Displays a line of characters
System.out.println(); // Displays a blank line
Product[] productArray = new Product[ 7 ]; // creates 7 product arrays
// adds data to the 7 arrays
productArray[0] = new Product();
productArray[0].setitemNumber(1);
productArray[0].setproductName ( "binder" );
productArray[0].setitemQuantity(15);
productArray[0].setitemPrice(1.5);
productArray[0].setinventoryValue(0);
productArray[1] = new Product();
productArray[1].setitemNumber(2);
productArray[1].setproductName( "paper" );
productArray[1].setitemQuantity(24);
productArray[1].setitemPrice(.95);
productArray[1].setinventoryValue(0);
productArray[2] = new Product();
productArray[2].setitemNumber(4);
productArray[2].setproductName( "pen" );
productArray[2].setitemQuantity(5);
productArray[2].setitemPrice(.25);
productArray[2].setinventoryValue(0);
productArray[3] = new Product();
productArray[3].setitemNumber(3);
productArray[3].setproductName( "marker" );
productArray[3].setitemQuantity(13);
productArray[3].setitemPrice(.5);
productArray[3].setinventoryValue(0);
productArray[4] = new Product();
productArray[4].setitemNumber(5);
productArray[4].setproductName( "pencil" );
productArray[4].setitemQuantity(28);
productArray[4].setitemPrice(.4);
productArray[4].setinventoryValue(0);
productArray[5] = new Product();
productArray[5].setitemNumber(7);
productArray[5].setproductName( "tape" );
productArray[5].setitemQuantity(14);
productArray[5].setitemPrice(1.65);
productArray[5].setinventoryValue(0);
productArray[6] = new Product();
productArray[6].setitemNumber(6);
productArray[6].setproductName( "staples" );
productArray[6].setitemQuantity(13);
productArray[6].setitemPrice(1.25);
productArray[6].setinventoryValue(0);
System.out.println( "Inventory listing prior to sorting by product name:" );
System.out.println(); // Displays a blank line
System.out.println( "Item #"+"\t"+"Product Name"+"\t"+"Stock"+"\t"+"Price"+"\t"+"Total Value"); // Displays a header line for the inventory array display
System.out.println(); // Displays a blank line
System.out.println( "--" ); // Displays a line of characters
System.out.println(); // Displays a blank line
for (int i=0; i<=6; i++)
{
Product products = productArray;
String productName = products.getproductName();
int itemNumber = products.getitemNumber();
int itemQuantity = products.getitemQuantity();
double itemPrice = products.getitemPrice();
double inventoryValue = products.getinventoryValue();
System.out.println( productArray.getitemNumber() +"\t"+ productArray.getproductName() +"\t"+"\t" + productArray.getitemQuantity() +"\t"+ nf.format(productArray.getitemPrice()) +"\t"+ nf.format(productArray.getinventoryValue()) );
}
Arrays.sort(productArray);
System.out.println( "--" ); // Displays a line of characters
System.out.println(); // Displays a blank line
System.out.println( "Inventory listing after being sorted by product name:" );
System.out.println(); // Displays a blank line
System.out.println( "Item #"+"\t"+"Product Name"+"\t"+"Stock"+"\t"+"Price"+"\t"+"Total Value"); // Displays a header line for the inventory array display
System.out.println(); // Displays a blank line
System.out.println( "--" ); // Displays a line of characters
for(int i=0; i <= 6; i++)
{
totalInventoryValue = totalInventoryValue + productArray.getinventoryValue(); // calculates the total value of the entire products inventory
System.out.println( productArray.getitemNumber() +"\t"+ productArray.getproductName() +"\t"+"\t"+ productArray.getitemQuantity() +"\t"+ nf.format(productArray.getitemPrice()) +"\t"+ nf.format(productArray.getinventoryValue()) );
}// end for
System.out.println(); // Displays a blank line
System.out.println( "The total value of the entire inventory is: " + nf.format(totalInventoryValue) ); // Displays the entire inventory value
System.out.println(); // Displays a blank line
System.out.println( "*****************************************************" ); // Displays a line of characters
} // end public static void main
}// end public class Inventoryprogrampart3 main
The following utilities have been set:
import java.io.*;
import java.text.*;
import java.util.Scanner;
import java.util.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
The program compiles, however when I try to run the program I receive the following error (which outputs about 1/2 way through the program:
Exception in thread "main" java.lang.ClassCastException: Product can not be cast to java language comparable.
(along with a listing of other errors - I can't even get my cut and paste to work on my command prompt window).
I've tried about 50 different iterations and nothing seems to work.
> Could you recommend a Java programming consultant
> that I could talk to? I must not be reading the same
> things that you are, or we are not speaking the same
> "language", as I am not making any progress.
>
> If I could work with someone who could answer my
> questions, I feel that I could get the information
> that I need.
Ok, let me just quote the relevant part of the tutorial you (supposedly) read:
[...]
How do we sort these Person instances by age or by name? Using the java.util.Arrays class' sort method, as in:
Arrays.sort(persons);
will throw a ClassCastException.
You can, of course, write your own code to sort them using an algorithm such as quick sort, bubble sort, or others, but that's impractical. The easy solution is to implement the java.lang.Comparable interface.
Using the java.lang.Comparable Interface
Implement the Comparable interface to make class instances comparable. This interface has one method, compareTo, which determines how to compare two instances of the class. The signature of this method is:
public int compareTo(Object o)
The compareTo method accepts Object, so you can pass it an instance of any type. However, chances are that you want to make sure to compare two instances of the same type. It does not make sense to compare an elephant with an ant, for example. Therefore, you can throw a java.lang.ClassCastException if the argument of this method is not the same type as your class.
The compareTo method returns zero if the object passed is equal to this instance. It returns a positive integer or a negative integer if this object is greater or smaller than the passed object, respectively.
Let's have a look at the examples in Listing 4 and Listing 5. Listing 4 presents a Person class that implements the Comparable interface. Notice that a Person object is older if its age value is greater than the object compared. Listing 5 shows the Testing class that constructs four instances of the Person class and sorts them by age. Both classes in Listings 4 and 5 reside in the comparable.ex01 package.
Listing 4: The Person Class That Implements the Comparable Interface
package comparable.ex01;
class Person implements Comparable {
private String firstName;
private String lastName;
private int age;
// prometheuzz: removed some obvious methods here
public int compareTo(Object anotherPerson) throws ClassCastException {
if (!(anotherPerson instanceof Person))
throw new ClassCastException("A Person object expected.");
int anotherPersonAge = ((Person) anotherPerson).getAge();
return this.age - anotherPersonAge;
}
}
[...]
Now look at your own object that you're trying to compare. In what ways does it differ from the Person class in the example?