Results 1 to 8 of 8
  1. #1
    xf021209 is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default It simply won't sort

    I have re-written a simple program and am trying to get an array of data to sort. The program runs fine without the Arrays.sort(); Once I add it in I get the following error:

    run:
    Exception in thread "main" java.lang.ClassCastException: InventoryMain.Inventory cannot be cast to java.lang.Comparable
    at java.util.Arrays.mergeSort(Arrays.java:1144)
    at java.util.Arrays.mergeSort(Arrays.java:1155)
    at java.util.Arrays.sort(Arrays.java:1079)
    at InventoryMain.InventoryMain.main(InventoryMain.jav a:69)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    This would indicated to me that the data in the array fields is not comparable, but I have no idea why. Here's the main class.

    Java Code:
    package InventoryMain;
    /**
     * @author Todd 
     */
    import java.util.Arrays;
    public class InventoryMain {
        public static void main(String[] args)
        {
            final int ItemArrayLength = 7; // set array length
                Inventory item[] = new Inventory[ItemArrayLength];
    
                Inventory item_1 = new Inventory();
                    item_1.setTitle("The Good Book");
                    item_1.setPublisher("Big Horn Publishing");
                    item_1.setSKU(28372);
                    item_1.setQTY(23);
                    item_1.setPRICE(29.95);
               item[0] = item_1;
    
                Inventory item_2 = new Inventory();
                    item_2.setTitle("Hold On Now");
                    item_2.setPublisher("The Liberal Press");
                    item_2.setSKU(53272);
                    item_2.setQTY(14);
                    item_2.setPRICE(39.95);
               item[1] = item_2;
    
                Inventory item_3 = new Inventory();
                    item_3.setTitle("Only The Lonely");
                    item_3.setPublisher("Big Horn Publishing");
                    item_3.setSKU(54352);
                    item_3.setQTY(38);
                    item_3.setPRICE(34.95);
               item[2] = item_3;
    
                Inventory item_4 = new Inventory();
                    item_4.setTitle("Cow's On Steroids");
                    item_4.setPublisher("Watchmen Press");
                    item_4.setSKU(98473);
                    item_4.setQTY(4);
                    item_4.setPRICE(12.95);
               item[3] = item_4;
    
                Inventory item_5 = new Inventory();
                    item_5.setTitle("Just A Little Longer");
                    item_5.setPublisher("Junk Publishing");
                    item_5.setSKU(23873);
                    item_5.setQTY(37);
                    item_5.setPRICE(16.87);
               item[4] = item_5;
    
                Inventory item_6 = new Inventory();
                    item_6.setTitle("Drafting");
                    item_6.setPublisher("Peterson Publishing, Inc.");
                    item_6.setSKU(52422);
                    item_6.setQTY(22);
                    item_6.setPRICE(24.95);
               item[5] = item_6;
    
                Inventory item_7 = new Inventory();
                    item_7.setTitle("Java for the Insane");
                    item_7.setPublisher("Crack Pot Press");
                    item_7.setSKU(31234);
                    item_7.setQTY(17);
                    item_7.setPRICE(79.95);
               item[6] = item_7;
    
               Arrays.sort(item);
    
            // Display Inventory for each Inventory object by calling the displayItemInfo from the Inventory class
            // Print this by array, not by each item_#
            System.out.print("Now printing items that used the constructor to initialize the instance variables\n---------------------------------------------------------------------------------");
    
                for (int count = 0; count < item.length; count++)
                {
                item[count].displayInventoryInfo();
                }
       }
    }
    Any suggestions would be greatly apprecated.

    Todd

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you dont tell java how to compare Inventory, how can java sort it?

    your Inventory class should implements comparable<Inventory>
    and implements compareTo(Inventory) method

  3. #3
    xf021209 is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    When I used Arrays.sort() in another program it sorted the array without any other code provided. I'm not sure what you're really meaning anyway. I thought that the Array.sort was telling Java to compare each item in the array with the other items.

    Todd


    Quote Originally Posted by mtyoung View Post
    if you dont tell java how to compare Inventory, how can java sort it?

    your Inventory class should implements comparable<Inventory>
    and implements compareTo(Inventory) method

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    another program it sorted... the array store int, long, String? or other object?

    you want to sort Inventory Array, Inventory is your self define Class... not Integer, String like build in Class

    Inventory class implements comparable<Inventory>
    and implements compareTo(Inventory) method
    or you may work out a Comparator and use Arrays.sort(T[] a, Comparator<? super T> c)
    Last edited by mtyoung; 04-25-2009 at 06:23 AM.

  5. #5
    xf021209 is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    I appreceate your efforts here but you're explaining something much more advanced and I don't understand. I've only been doing this programming for seven weeks now. Thanks anyway,

    Todd



    Quote Originally Posted by mtyoung View Post
    if you dont tell java how to compare Inventory, how can java sort it?

    your Inventory class should implements comparable<Inventory>
    and implements compareTo(Inventory) method
    Quote Originally Posted by mtyoung View Post
    another program it sorted... the array store int, long, String? or other object?

    you want to sort Inventory Array, Inventory is your self define object ... not Integer, String like build in object

    Inventory class implements comparable<Inventory>
    and implements compareTo(Inventory) method
    or you may work out a Comparator and use Arrays.sort(T[] a, Comparator<? super T> c)

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    explaining something much more advanced?
    if someone give you the hints, go for searching...
    we do not know anything when we were born
    it is learning... from nothing to something...

    according to Arrays (Java Platform SE 6)
    as Inventory is self define Class
    sorting the whole array may use
    Arrays.sort(Object[] a)
    public static void sort(Object[] a)
    Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, ....
    or
    Arrays.sort(T[] a, Comparator<? super T> c)
    Sorts the specified array of objects according to the order induced by the specified comparator.
    ....
    example page
    Java Sorting: Comparator vs Comparable Tutorial - Computerized World
    Last edited by mtyoung; 04-25-2009 at 06:25 AM.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Various library classes such as Strings and number classes are "naturally sortable". Strictly, what that means as that their class definitions indicate how the items are sorted. They do that by implementing the Comparable interface-- this basically means adding a compareTo() method to your class that defines the ordering of one object of your class with another.

    If you create your own class of objects that you want to sort, then you need to indicate how you want objects of your class to be sorted. For example, if you had a class called Employee with name fields, age field etc, Java can't "magically" know that you want to sort by surname or by age. You have to define this.

    I have an example of how to implement Comparable to make your items sortable that may help you.

  8. #8
    xf021209 is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    Thank you very much neilcoffey, that explanation is exactly what I needed. I've come up with the following in my main class:
    Java Code:
         Arrays.sort(item, new SortClass());
    and then I created the SortClass:

    Java Code:
    package inventorymain;
    
    import InventoryMain.Inventory;
    import java.util.Comparator;
    
    public class SortClass implements Comparator
    {
        public int compare(Object UnsortItem, Object SortItem)
        {
         String bookName1 = ((Inventory) UnsortItem).getTitle().toUpperCase();
         String bookName2 = ((Inventory) SortItem).getTitle().toUpperCase();
    
         if (!(bookName1.equals(bookName2)))
             return bookName1.compareTo(bookName2);
         else
             return bookName2.compareTo(bookName1);
        }
    }
    It works like a champ and now I understand how to do this.

    Greatly appreceated!

    You and a few others need to get over onto liveperson.co_ and become Experts, get paid for your support. You, Fubarable and a couple of others have been great at not only pointing out the problem but explaining how to resolve it.

    Thanks again!

    Todd

Similar Threads

  1. input to an integer (simply)
    By chitwood in forum Advanced Java
    Replies: 3
    Last Post: 03-18-2009, 07:34 AM
  2. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  3. Error With Simply Displaying Picture
    By JDCAce in forum Java Applets
    Replies: 4
    Last Post: 10-07-2008, 09:09 PM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  5. Simply output the result to a text file.
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •