Results 1 to 10 of 10
  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default How to sort arrays

    Hi there.

    I have tried very hard to make the bubblesort work for my program. I cannot get my program to run. I have tried everything I can think of. So, what is another way I can sort my array and have it print one line at a time? Here is my code:

    Java Code:
    	public class InventoryAdvanced {
    
    
    	public static void main(String args[]){
    
    		int size = 4;
    
    		InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10,13.00);
    		InventoryDefine hercules = new InventoryDefine("Hercules", 2, 20, 5.00);
    		InventoryDefine mermaid = new InventoryDefine("The Little Mermaid", 3, 30, 10.00);
            InventorySort methodCall = new InventorySort();
    
       		InventoryDefine[] inventoryArray = new InventoryDefine[3];
    
       		inventoryArray[0] = swordstone;
       		inventoryArray[1] = hercules;
       		inventoryArray[2] = mermaid;
    
       		//Call the sort method
       		methodCall.Sort(inventoryArray);
    
       		//Display the sorted array
       		for (int i = 0; i < size; i++){
    
       			System.out.println(inventoryArray[i]);
    
       		}
    
    
    	}
    }
    Java Code:
    /**
     * @(#)InventoryAdvanced.java
     *
     * InventoryAdvanced application
     *
     * @author
     * @version 1.00 2012/1/26
     */
    
    public class InventoryDefine {
    
    	//Define the variables
    	public String inventoryName;
    	public int inventoryNumber;
    	public int inventoryStock;
    	public double inventoryPrice;
    
    	//Declare the constructor
    	public InventoryDefine(String name, int productNumber, int productStock, double productPrice){
    
    		this.inventoryName = name;
    		this.inventoryNumber = productNumber;
    		this.inventoryStock = productStock;
    		this.inventoryPrice = productPrice;
    
    	}
    
    	public int getNumber(){
    
    		return inventoryNumber;
    	}
    }
    Java Code:
    /**
     * @(#)InventorySort.java
     *
     *
     * @author
     * @version 1.00 2012/1/28
     */
    
    
    public class InventorySort {
    
    	public void Sort(InventoryAdvanced[] array){
    
    		int a, b, t;
    		int size;
    
    		size = 4;
    
    		for (a=1; a < size; a++)
    			for (b=size-1; b >= a; b--){
    
    				if(array[b-1] > array[b]) {
    
    					t = array[b-1];
    					array[b-1] = array[b];
    					array[b] = t;
    
    				}
    			}
    	}
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: How to sort arrays

    the object you want to sort has 4 fields. which of them should be used to sort?

  3. #3
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to sort arrays

    I am trying to sort by the product number. :)

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: How to sort arrays

    ok and the other question: do you have to write your own bubblesort or it's possible to implement the Comparable interface?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How to sort arrays

    if(array[b-1] > array[b]) {
    What is this if statement comparing? You need to specify which field is to be compared.

  6. #6
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to sort arrays

    In the Sort method parameter I placed array as the parameter. So, it is comparing the inventoryArray.

    @J2me64: I think I can use comparable interface. Can you explain how I implement that into my code? I don't know much about it. Thank you!

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How to sort arrays

    it is comparing the inventoryArray.
    What your code is comparng is the values of two references to InventoryAdvanced objects. It is not comparing the contents of the objects. Think about when you compare Strings, you use the equals() method, not the == operator.
    Same thing here. You must either use a method(the better way) or a field in the class.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: How to sort arrays

    ok, forget the class InventorySort and change the class InventoryDefine to

    1. public class InventoryDefine implements Comparable <InventoryDefine>
    2. the interface Comparable want you to implement a method called compareTo(InventoryDefine idefine) with the parameter of the type given in 1. with Comparable<YourClass>
    3. the method compareTo can be implemented like this

    Java Code:
        public int compareTo(InventoryDefine id) {
        	return this.inventoryNumber - id.inventoryNumber;
        }
    the return value is positive if the inventoryNumber is greater then the id.inventoryNumber and negative vice versa and zero if both values are equal. for more details look here: Comparable (Java Platform SE 6). all this is done by simply subtracting the id.inventoryNumber from this.inventoryNumber. for comparing Strings you must use the method compareTo from the String class.

    4. inside the class InventoryAdvanced just before the for-loop you can simple insert this line: Arrays.sort(inventoryArray); for ascending sort.
    5. to check if the sort is ok you can override the toString() in the class InventoryDefine to

    Java Code:
       public String toString() {
        	return this.inventoryName + " " + this.inventoryNumber;
        }
    hope this helps.
    Last edited by j2me64; 01-31-2012 at 09:19 PM.

  9. #9
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to sort arrays

    Thank you so much! :) Question:

    Does the comparable sort the arrays? Cause I tried sorting the array with comparable and I get this error:

    method compareTo in class InventoryDefine cannot be applied to given types.

    Here is the code:

    Java Code:
    /**
     * @(#)InventoryAdvanced.java
     *
     * InventoryAdvanced application
     *
     * @author
     * @version 1.00 2012/1/26
     */
    
    public class InventoryDefine implements Comparable <InventoryDefine> {
    
    	//Define the variables
    	public String inventoryName;
    	public int inventoryNumber;
    	public int inventoryStock;
    	public double inventoryPrice;
    
    	//Declare the constructor
    	public InventoryDefine(String name, int productNumber, int productStock, double productPrice){
    
    		this.inventoryName = name;
    		this.inventoryNumber = productNumber;
    		this.inventoryStock = productStock;
    		this.inventoryPrice = productPrice;
    
    	}
    
    	public int compareTo(InventoryDefine id) {
        return this.inventoryNumber - id.inventoryNumber;
    }
    }
    Java Code:
    import java.util.Arrays;
    
    	public class InventoryAdvanced {
    
    
    	public static void main(String args[]){
    
    		int size = 4;
    
    		InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10,13.00);
    		InventoryDefine hercules = new InventoryDefine("Hercules", 2, 20, 5.00);
    		InventoryDefine mermaid = new InventoryDefine("The Little Mermaid", 3, 30, 10.00);
         	InventoryDefine test = new InventoryDefine("test",0,0,0);
    
       		InventoryDefine[] inventoryArray = new InventoryDefine[3];
    
       		inventoryArray[0] = swordstone;
       		inventoryArray[1] = hercules;
       		inventoryArray[2] = mermaid;
    
    		test.compareTo(inventoryArray);
    
    
       		//Display the sorted array
       		for (int i = 0; i < size; i++){
    
       			System.out.println(inventoryArray[i]);
    
       		}
    
    
    	}
    }
    Last edited by BenH; 01-31-2012 at 09:44 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How to sort arrays

    @j2me64 You need to explain to the OP what you are doing. I don't think he understands your post and the fact that he needs to tailor the class to be sorted so that it will work with one of the Java SE utility classes that will do the sorting for him.

Similar Threads

  1. Bubble Sort For 2D Arrays
    By tmantonym in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 09:40 PM
  2. using selection sort on 2 string arrays
    By augiechimpo in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 04:59 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Arrays.Sort usage
    By parthpatel in forum New To Java
    Replies: 2
    Last Post: 02-14-2010, 12:26 PM
  5. How to sort 2 arrays together
    By masaka in forum New To Java
    Replies: 13
    Last Post: 05-21-2008, 04:36 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
  •