简体   繁体   中英

Sorting array list

public class Saleitem {
    public Product product = null;
    public int numberofproduct = 0;

    static ArrayList<Saleitem> Saleitemarray = new ArrayList<Saleitem>();       
    static ArrayList<Integer[]> total = new ArrayList<Integer[]>();
//read the sales data
public static void salesData() {
    String SalesDataCSV = "SalesData.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    System.out.println("\nThe Sales Data file has been opened\n");

    try {
        int currentcustomer = 1;
        int lastcustomer = 1;
        double sum = 0;

        br = new BufferedReader(new FileReader(SalesDataCSV));
        line = br.readLine();

        System.out.println("-----------------------------------------------");
        System.out.println("Sales Data File");
        System.out.println("Customer ID, Product ID, Number of Units");
        System.out.println("-----------------------------------------------");
        while ((line = br.readLine()) != null) {
            String field[] = line.split(cvsSplitBy);        
            if(field.length>1) {
                String currentcustomerID = field[0];
                String currentproductID = field[1];
                String currentunitnumber = field[2];
                Product currentproduct = null;                              

                currentcustomer = Integer.parseInt(currentcustomerID);
                int currentproductid = Integer.parseInt(currentproductID);
                int currentproductunit = Integer.parseInt(currentunitnumber);

                //-------------------------------------
                //   START OF PRODUCT/SALE ITEM PROCESSING
                //-------------------------------------  
                System.out.println(currentcustomer   +   " ,   "   +  currentproductid   +   " ,   "   +   currentproductunit);


                ////////////////////
                if (lastcustomer == currentcustomer) {

                    Saleitem salesItemObject = new Saleitem(currentproductid, currentproductunit,
                            Product.getUnitPrice(currentproductid));
                    Saleitemarray.add(salesItemObject);

                } else {

                    // sale receipt date, time, etc.
                    Salereceipt salesReceiptObject = new Salereceipt(lastcustomer, lastcustomer,
                    sum, "2/20/16", (int) (Math.random() * 2000));

                    Salereceipt.receipt.add(salesReceiptObject);
                    lastcustomer = currentcustomer;

                    Saleitemarray.clear();
                    sum = 0;
                }
                ///////////////////////////

                //Find the correct product that the customer ordered
                for (int i = 0; i < Product.productData.size(); i++){
                    if (((Product.productData).get(i)).productID == currentproductid){
                        currentproduct = Product.productData.get(i);
                    }
                }
                Saleitem salesItemObject = new Saleitem(currentproduct, currentproductunit);

                Saleitemarray.add(salesItemObject);
                boolean found = false;
                //update total
                for (int i = 0; i < total.size(); i++){

                    //total is an array of arrays =)

                    //in the array, index 0 is the productID 
                    // index 1 is the total sold of that product
                    //Find the correct product total
                    if ((total.get(i))[0] == salesItemObject.product.productID){
                        //if we found it then we will mark found
                        //so that we can add in the item if it doesnt exist
                        //in our total array
                        found = true;
                        //increment the total number of prodcuts sold
                        (total.get(i))[1] += salesItemObject.numberofproduct;
                    }                      
                }

                if (found == false){
                    Integer[] array = new Integer[2];

                    // index 0 = product id
                    // index 1 = total number of products sold

                    array[0] = salesItemObject.product.productID;
                    array[1] = salesItemObject.numberofproduct;

                    total.add(array);
                }

                //-------------------------------------
                //   END OF PRODUCT/SALE ITEM PROCESSING
                //-------------------------------------    
                //this is done inside of the constructor

                if (currentcustomer == lastcustomer){
                    sum += currentproduct.productPrice * currentproductunit;                      
                }                    
            }   
        }

The Sales Data is imported from a file that has Customer_ID[0], Product_ID[1], Units_ordered[2] I want to sort the ArrayList total by the Product_ID in ascending order. What would be the best way to do this. Im new to java so I don't know much of the syntax.

You can use Collections#sort like below.

Add a getter for ProductId and you're done

Collections.sort(total, new Comparator<Saleitem>(){
    @Override
    public int compare(Saleitem s1, Saleitem s2) {
        return s1.getProductId() - s2.getProductId();
    }
});
total.sort((item1, item2) -> item1.getProductId() - item2.getProductId());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM