簡體   English   中英

排序數組列表

[英]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;                      
                }                    
            }   
        }

銷售數據是從具有Customer_ID [0],Product_ID [1],Units_ordered [2]的文件中導入的。我想按Product_ID的升序對ArrayList總數進行排序。 什么是做到這一點的最佳方法。 我是Java的新手,所以我不太了解語法。

您可以像下面那樣使用Collections#sort

ProductId添加一個吸氣劑就可以了

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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM