Results 1 to 10 of 10
Thread: How to sort arrays
- 01-31-2012, 07:56 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
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; } } } }
- 01-31-2012, 08:39 PM #2
Re: How to sort arrays
the object you want to sort has 4 fields. which of them should be used to sort?
- 01-31-2012, 08:46 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
Re: How to sort arrays
I am trying to sort by the product number. :)
- 01-31-2012, 08:49 PM #4
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?
- 01-31-2012, 08:52 PM #5
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.
- 01-31-2012, 09:02 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
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!
- 01-31-2012, 09:06 PM #7
Re: How to sort arrays
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.it is comparing the inventoryArray.
Same thing here. You must either use a method(the better way) or a field in the class.
- 01-31-2012, 09:16 PM #8
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
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.Java Code:public int compareTo(InventoryDefine id) { return this.inventoryNumber - id.inventoryNumber; }
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
hope this helps.Java Code:public String toString() { return this.inventoryName + " " + this.inventoryNumber; }Last edited by j2me64; 01-31-2012 at 09:19 PM.
- 01-31-2012, 09:35 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
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.
- 01-31-2012, 09:44 PM #10
Similar Threads
-
Bubble Sort For 2D Arrays
By tmantonym in forum New To JavaReplies: 2Last Post: 11-30-2011, 09:40 PM -
using selection sort on 2 string arrays
By augiechimpo in forum New To JavaReplies: 5Last Post: 03-27-2011, 04:59 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Arrays.Sort usage
By parthpatel in forum New To JavaReplies: 2Last Post: 02-14-2010, 12:26 PM -
How to sort 2 arrays together
By masaka in forum New To JavaReplies: 13Last Post: 05-21-2008, 04:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks