-1

I am trying to create a program that will take orders from the user. My problem is that I cannot sort the foods alphabetically because I have 3 separate list boxes, 1st list box is for the name of the food, 2nd for the quantity of the food, and the 3rd list box is for the price of the food. What I want is for the 2nd and 3rd list box to follow the 1st list box so for example if list boxes contains Orange(2pcs)($1), Melon(3pcs)($2), Apple(1pc)($4)

How it looks like:

Orange    |    2pcs    |    $1
Melon     |    3pcs    |    $2
Apple     |    1pcs    |    $4

Then after pressing the "Sort" button I want it to look like this:

Apple     |    1pcs    |    $4
Melon     |    3pcs    |    $2
Orange    |    2pcs    |    $1

I do not know what to do with the 2nd and the 3rd list box after sorting the 1st list box. I do not know how to make the 2nd and the 3rd one follow the order of the 1st list box.

http://postimg.org/image/awxe4pk4t/ User will type in the food name and choose in the combo box the desired quantity. Then after pressing the add to cart button the program will add the input to the list boxes the price will appear on the 3rd list box according to the food that the user typed in. There are only 10 types of food declared in this program.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190

1 Answers1

0

You can put all your params into structure, like this:

public class OrderItem {

    public String foodName;
    public int price;
    public int quantity;

}

And use List<OrderItem> instead of three lists of params;

Than sort it with:

List<OrderItem> list = new ArrayList<>();
...

Collections.sort(list, new Comparator<OrderItem>() {
    @Override
    public int compare(OrderItem item1, OrderItem item2) {
        return item1.foodName.compareTo(item2.foodName);  
    }

});

After this put params into your list boxes separately;

You can also use getters and setters for OrderItem fields, instead of public fields, but it's the simplest way.

CupOfWonder
  • 58
  • 1
  • 6