1

I have this arrayList that i need to first sort from greatest to least by both item price and then after i need to sort it by the amount of items sold. Whats the easiest way to accomplish this? thanks!

 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);
asked May 2, 2014 at 2:13

1 Answer 1

3

Write a custom comparator:

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;
 }
}

Edit (thanks to @Levenal):

Then use Collections.sort() to sort your list.

answered May 2, 2014 at 2:19

2 Comments

Once you have the Comparator I believe it is Collections.sort(...) that you can use to order your list?
@user3579060 You put the comparator in a new class file. You put the Collections.sort() call wherever it is that you want to sort your list of salesPerson objects.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.