I am also looking for potential ways that I can incorporate a for loop.
I am learning Java on my own and am terribly confused on how I can sort the below array by type and then by price. This question is not similar to one that has been previously posted, because the question flagged only involves Strings, while mine uses a combination of ints, Strings, and doubles. All of the past posts I have looked at on Overflow before making my own post have not involved doubles in any way.
This is what I have defined Item as.
public Item(int type, String name, double quantity, double price) {
this.type = type;
this.name = name;
this.quantity = quantity;
this.price = price;
}
This is my array:
public static void main ()
{Item [] shoppingItems = {new Item(2,"Cherry",9,1.35),
new Item(3,"Orange Juice",4,5.29),
new Item(5,"Hand Soap",2,1.77),
new Item(6,"Tooth Brush",3,4.55),
new Item(4,"Cupcake",3,2.95),
new Item(1,"Red Tomato Sauce",5.5,2.35),
new Item(3,"Chicken",1.9,2.48),
new Item(3,"Apple Pie",2,3.99),
new Item(7,"Bug Spray",1,9.28),
new Item(3,"Roast Beef",2.82,5.99),
new Item(5,"Light Bulb",3,3.92),
new Item(4,"Cookies",0.2,2.96),
new Item(2,"Watermelon",1.8,2.29)
};
}
How can I sort this array in ascending order by type? And also by price?
I looked into using Comparators, but they did not seem to work for my objective. I'm also not sure because price is a double.