gpt4 book ai didi

java - 如何对 ArrayList Java 进行排序

转载 作者:行者123 更新时间:2023-11-29 05:24:02 25 4
gpt4 key购买 nike

我有这个 arrayList,我需要先按商品价格从大到小排序,然后再按售出商品数量排序。完成此操作的最简单方法是什么?谢谢!

 import java.util.*;
public class salesPerson {

//salesPerson fields
private int salespersonID;
private String salespersonName;
private String productType;
private int unitsSold = 0;
private double unitPrice;

//Constructor method
public salesPerson(int salespersonID, String salespersonName, String productType, int unitsSold, double unitPrice)
{
this.salespersonID = salespersonID;
this.salespersonName = salespersonName;
this.productType = productType;
this.unitsSold = unitsSold;
this.unitPrice = unitPrice;
}


//Accessor for salesPerson
public int getSalesPersonID()
{
return salespersonID;
}

public String getSalesPersonName()
{
return salespersonName;
}

public String getProductType()
{
return productType;
}

public int getUnitsSold()
{
return unitsSold;
}

public double getUnitPrice()
{
return unitPrice;
}

public double getTotalSold()
{
return unitsSold * unitPrice;
}

//Mutoators for salesPerson
public void setSalesPersonID(int salespersonID)
{
this.salespersonID = salespersonID;
}

public void setSalesPersonName(String salespersonName)
{
this.salespersonName = salespersonName;
}

public void setProductType(String productType)
{
this.productType = productType;
}

public void setUnitsSold(int unitsSold)
{
this.unitsSold += unitsSold;
}

public void setUnitProce(double unitPrice)
{
this.unitPrice = unitPrice;
}

public static void main(String[] args)
{
ArrayList<salesPerson> salesPeople = new ArrayList<salesPerson>();
Scanner userInput = new Scanner(System.in);
boolean newRecord = true;
boolean showReport = true;

do
{
int salespersonID;
String salespersonName;
String productType;
int unitsSold = 0;
double unitPrice;



System.out.println("Please enter the Salesperson Inoformation.");
System.out.print("Salesperson ID: ");
salespersonID = userInput.nextInt();

System.out.print("Salesperson Name: ");
salespersonName = userInput.next();
System.out.print("Product Type: ");
productType = userInput.next();
System.out.print("Units Sold: ");
unitsSold = userInput.nextInt();
System.out.print("Unit Price: ");
unitPrice = userInput.nextDouble();

if(salesPeople.size() == 0)
{
salesPerson tmp = new salesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(tmp);
}
else
{
for(int i=0; i < salesPeople.size(); i++)
{
if(salesPeople.get(i).getSalesPersonID() == salespersonID)
{
salesPeople.get(i).setUnitsSold(unitsSold);
break;
}
else
{
salesPerson tmp = new salesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(tmp);
break;
}
//System.out.println(salesPeople.get(i).getSalesPersonName());
}
}

System.out.print("Would you like to enter more data?(y/n)");
String askNew = userInput.next();
newRecord = (askNew.toLowerCase().equals("y")) ? true : false;

}
while(newRecord == true);

最佳答案

编写自定义比较器:

public SalesPersonComparator implements Comparator<salesPerson> {
public int compare(final salesPerson p1, final salesPerson p2) {
// get the comparison of the unit prices (in descending order, so compare p2 to p1)
int comp = new Double(p2.getUnitPrice()).compareTo(new Double(p1.getUnitPrice()));

// if the same
if(comp == 0) {
// compare the units sold (in descending order, so compare p2 to p1)
comp = new Integer(p2.getUnitsSold()).compareTo(new Integer(p1.getUnitsSold()));
}
return comp;
}
}

编辑(感谢@Levenal):

然后使用 Collections.sort() 对列表进行排序。

关于java - 如何对 ArrayList Java 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com