PDA

View Full Version : Sort array of objects



SMartin91
04-22-2009, 12:44 AM
Hey there, I'm looking in to using the sort method for arrays. I have a class of grocery items and 3 arrays of 25 items. I need to sort one by the aisle number, another by price, and the last alphabetically. Can I do this with my class or do I need to alter it in some way or should I write my own sorting method?



class GroceryItem{

public String name;
public double price;
public int stockNumber, orderNumber, row, numItems;
public long SKU;
Random generator = new Random();

public GroceryItem(String name, String price, String row){
this.name = name;
this.price = Double.parseDouble(price);
this.row = Integer.parseInt(row);
this.stockNumber = generator.nextInt(50);
this.orderNumber = generator.nextInt(20);
this.SKU = generator.nextInt(1000000);
}
public void purchase(){
this.stockNumber -= 1;
this.numItems += 1;
}
}



Thank you for any help you have to offer!